Load the following example in different browsers.
<!DOCTYPE html>
<html>
<body>
<a onclick='document.write("text <a href=\"#a\">link</a>");document.close();'>click</a>
</body>
</html>
When the page is loaded click the link. Doing this will rewrite the page using document.write which will contain an anchor named "link".
In IE8, IE9, Chrome latest when this link is clicked it will not lead to page load.
In Firefox (tested with latest and FF6) clicking the link reloads the original page.
Firefox behavior seems to be incorrect as using anchors should not lead to page loads. If document write is not used clicking on anchors won't lead to page load even in Firefox.
Is there a workaround for this?
The goal would be to use document.write. This sample just simulates that we'd like to load another complete webpage including lot of javascript code with AJAX which needs to run properly after the inclusion.
The spec for write()
is somewhat unclear in that regard:
Write a string of text to a document stream opened by open().
Unclear being: What should happen if the stream is not open yet/anymore?
Writing to a document that has already loaded without calling document.open() will automatically perform a document.open call.
As this is effectively creating (read: "loading") a new document, hence a navigation and page load is performed. There is no way around this in Firefox. You could of course file a bug and ask for Chrome/IE parity for the sake of an open web.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments