简体   繁体   English

使用document.write后Internet Explorer的window.resize失败

[英]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.

相关问题 在 Internet Explorer 中触发 window.resize 事件 - window.resize event firing in Internet Explorer 为什么这个JavaScript(使用document.open和document.write)不能在Internet Explorer或Opera中运行? - Why won't this JavaScript (using document.open and document.write) work in Internet Explorer or Opera? document.write(“ anything”)不会作为外部脚本在Internet Explorer中执行 - document.write(“anything”) does not execute in internet explorer as an external script 使用jQuery window.resize - window.resize using jquery jquery 如何写 if (window.width() &lt; 1200 &amp;&amp; after window.resize() window.width() &gt; 1200){} - jquery how to write if (window.width() < 1200 && after window.resize() window.width() > 1200){} 在 document.write() 创建的新文档中调用 history.pushState() 会导致 Internet Explorer 11 上的 SecurityError - Calling history.pushState() inside a new document created by document.write() causes SecurityError on Internet Explorer 11 为什么这个document.write iframe广告代码完全打破了Internet Explorer? - Why does this document.write iframe ads code completely break Internet Explorer? Highcharts中的window.resize() - window.resize() in Highcharts window.resize不在滚动 - window.resize not on scrolling 在iOS9下打开的窗口中使用document.write - Using document.write in an opened window under iOS9
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM