Skip to main content

Bug Tracker

Side navigation

#919 closed bug (invalid)

Opened February 05, 2007 05:55PM UTC

Closed February 07, 2007 03:29PM UTC

Last modified February 09, 2007 10:22AM UTC

Dom ready premature execution in IE

Reported by: Arrix Owned by:
Priority: major Milestone:
Component: ajax Version: 1.1
Keywords: dom ready domready Cc:
Blocked by: Blocking:
Description

$(document).ready(fn) may prematurely call fn if innerHTML modification is performed before the document finishes loading.

This happens in IE6 and IE7.

Firefox and Opera don't have the problem.

I haven't tested with Safari yet.

jQuery revision: 1261

Observation: Run the page in IE, refresh and again. It will alert 0 in most times.

sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery domReady premature execution</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function() {
    alert($('#div2').size()); //expected to alert 1
});
</script>
</head>

<body>
<div id="div1">div1</div>
<script type="text/javascript">
var d = document.getElementById('div1');
d.innerHTML = d.innerHTML; //innerHTML modification before page loads
</script>

<div id="div2">div2</div>
</body>

</html>
Attachments (0)
Change History (2)

Changed February 07, 2007 03:29PM UTC by brandon comment:1

resolution: → invalid
status: newclosed

The DOM ready event was created to prevent the above inline code all together.

The fix for this is to actually *use* the document.ready event instead of modifying the DOM before it is ready.

Changed February 09, 2007 10:22AM UTC by Arrix comment:2

I see. It's perfect when you have full control of every piece of javascript code.