[webkit-dev] Timer callback and 'this' object

Dmitry Titov dimich at chromium.org
Mon Dec 1 17:08:17 PST 2008


Hi webkit-dev,

I'm looking at JS timeout code and found a callback behavior that seems
strange. Not sure if it is intentional.
If timeout is set on a 'window' object from another frame and timeout
callback is specified as a JS string, it is executed "inside the frame"...
So the context of execution is different depending how the callback is
specified.

Here is html (also attached) that loads an empty IFRAME and sets 2 timeouts
on that iframe's window - as a JS closure and as JS string. In former case,
the callback runs in the main page (expected, since the JS closure
'captures' the context in which it is defined), in the latter- inside iframe
(a bit weird?). So the text goes in both "body" elements.

Is there a reason why JS string is evaluated in the inner context of the
iframe? Would a test verifying this behavior be a good thing? Curiously, IE
and FF behave the same way.

<html>
<script>
function test() {
  // Get the iframe's window.
  var iframeWindow = window.frames["testIframe"];

  // setTimeout with a closure as callback.
  iframeWindow.setTimeout(
    function() {
      if (!document.body)
        document.write('<body></body>');
      document.body.appendChild(document.createTextNode('Timer 1
fired.'));}, 1);

  // setTimeout with a JS string containing similar code.
  iframeWindow.setTimeout(
     "if (!document.body)" +
     "  document.write('<body></body>');" +
     "document.body.appendChild(document.createTextNode('Timer 2
fired.'));", 1);
}
</script>
<body onLoad="test();">
<iframe src="about:blank" id="testIframe"></iframe>
</body>
</html>


Thanks,
Dmitry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-dev/attachments/20081201/cb11b653/attachment.html>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-dev/attachments/20081201/cb11b653/attachment-0001.html>


More information about the webkit-dev mailing list