#170 new
gleneivey

Scope of body.onload handler is root window context, even for HTML loaded into iframes

Reported by gleneivey | May 5th, 2010 @ 04:50 PM

This problem is demonstrated by the "Scoping of JS inline in HTML" test in specs/scope/spec.js. This test produces:

 [java] [PASS](35){scope|Scoping of JS inline in HTML} Can get content from dynamically-generate p element
 [java] [PASS](36){scope|Scoping of JS inline in HTML} Got confirmation of access to 'document' object in iframe
 [java] [FAIL](37){scope|Scoping of JS inline in HTML} Died on test [#3](/projects/21590/tickets/3 "Ticket #3"): Cannot read property "innerHTML" from null

The bug occurs because there is code in specs/fixtures/scope/iframeXa.html that defines a function that is called as the body.onload handler (set as an attribute in the HTML), but when the onload handler is to be run, the lookup for the function named in the attribute occurs in a scope other than the window object within the iframe, so the function isn't found. (Test runs fine in browser, of course.)

It can be made to run by changing specs/fixtures/scope/iframeXa.html to be:

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

<!-- * This file is a component of env.js, * http://github.com/thatcher/env-js/ * a Pure JavaScript Browser Environment * Copyright 2010 John Resig, licensed under the MIT License * http://www.opensource.org/licenses/mit-license.php --> <!-- elements and scripting here match test cases in ../../scope/spec.js -->
  lang="en" dir="ltr" id="html">
<title>Content document for IFRAME element in env.js unit test suite</title>
<p id="anElementWithText">
  This is the text content of a paragraph element.
</p>

<script type="text/javascript">
  //ensure that we can execute JS inline while loading
  document.write(
    '\x3cp id="js_generated_p"\x3eDynamically-generated\x3c/p\x3e');
</script>

<script type="text/javascript">
  //verify that our "document" object actually points to this page
  document.write('\x3cp id="internalDocRefResult"\x3e');
  if (document.getElementById('anElementWithText'))
    document.write("First paragraph element exists-found.");
  else
    document.write("Eeek!  Didn't find paragraph id=anElementWithText.");
  document.write('\x3c/p\x3e');
</script>

<script type="text/javascript">
  // append 'p id=appended "An appended paragraph"' to doc....

// function checkEventsWithinIframe(){ top.checkEventsWithinIframe = function(){
top.print(" Hello Body.OnLoad ");

    var t = document.createTextNode("An appended paragraph");
    var p = document.createElement("p");
    p.setAttribute("id", "appended");
    p.appendChild(t);
    document.getElementsByTagName('body')[0].appendChild(p);
  };
</script>

To verify this diagnosis of the bug, remove the "top.checkEvents..." line and un-comment the line above it, and the "Ooops" print in the will be executed when the test is run.

No comments found

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

a javascript browser environment

People watching this ticket

Pages