Reputation: 5200
I have developed a JS library that renders layouts (similar to layout rendering as in Razor View Engine in ASP.Net MVC). In order to display the final result, I replace the whole document by calling the document.write(...)
function. For example:
document.open()
document.write(renderedLayout)
document.close()
I'm using this methods because I need all scripts whether defined by the developer or acquired from an external resource to be run and evaluated right after the content is replaced. That said, replacing the inner HTML of the html
node will fail.
The methods I described works in Opera, Chrome, and Firefox but when I try to test it under IE and Edge, it fails and the browser does not showing anything once the write
method is called.
My test shows that, all console outputs are received just before write
is called and after that everything disappears.
Even though some may suggest that it could be a security measure in IE or Edge which might be solved by changing their configs, I would like to know how I can solve it using pure JavaScript so that my solution could be used in all major browsers.
I have traced and tested my code carefully, and I'm 100% sure the block of code I attached in the question is causing the blocking.
Upvotes: 0
Views: 1702
Reputation: 121
document.write writes to the document stream, calling document.write on a closed (loaded) document automatically calls document.open, which will clear the document.
removing document.open() and document.close() might help.
Next time when you are confused if your function written works on browser, Open console(use ctrl+i or F12) - In Console place your Javascript code.
Here is the reference for you : https://developer.mozilla.org/en-US/docs/Web/API/Document/write
Upvotes: 1