[英]Internet explorer window.resize fails after using document.write
This may be a bug in internet explorer (IE9), wondering if it is known or there is a workaround. 这可能是Internet Explorer(IE9)中的错误,想知道是否已知或存在解决方法。
It can be reproduced by opening up the dev console, and executing each of these commands, 可以通过打开开发控制台并执行以下每个命令来复制该代码:
window.onresize = function() { alert("onresize"); }
At this point, when you resize the window, the event fires as expected. 此时,当您调整窗口大小时,事件将按预期触发。 Now execute,
现在执行
document.write("test");
After this point window.resize
will be null. 在此之后,
window.resize
将为null。
That's probably expected since document.write
calls document.open
which clears everything. 这可能是预料之中的,因为
document.write
调用document.open
会清除所有内容。
However, even if you add back an event handler, it will never fire, 但是,即使您添加回事件处理程序,它也永远不会触发,
window.onresize = function() { alert("onresize"); }
You have to write the whole script block into the new document. 您必须将整个脚本块写入新文档中。 For example:
例如:
document.write("<div>some html</div><script>window.onsize=function() { alert('onresize'); } </script>
This is because when you call document.write it creates a new document with an "empty" url. 这是因为当您调用document.write时,它将使用“空” URL创建一个新文档。 Where as your original document comes from a different Url (for example, " http://www.yoursite.com/Page1.aspx ". The site for the empty Url is not the same as the site of the original Url. And a browser does not allow script in pages from one site calls script in pages from another site (to avoid cross site script attack). Thus it is not possible for you to call a function defined in " http://www.yoursite.com/Page1.aspx " from a page with null Url. As such you have to write the script code itself into the new document.
原始文档来自不同的网址(例如,“ http://www.yoursite.com/Page1.aspx ”。),空网址所在的位置与原始网址不同。浏览器不允许一个站点的页面中的脚本调用另一站点的页面中的脚本(以避免跨站点脚本攻击)。因此,您无法调用在“ http://www.yoursite.com/网址为空的页面中的“ Page1.aspx ”。因此,您必须将脚本代码本身写入新文档中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.