简体   繁体   中英

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.

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.

That's probably expected since document.write calls document.open which clears everything.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM