4

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.

phantasm
  • 105
  • 1
  • 6
  • 5
    The workaround would be to not use `document.write`. I'm sure there is another (much saner) way to achieve what you want. – user247702 Sep 04 '13 at 15:40
  • Probably firefox does interpret the missing `href` attribute differently. Using `document.write` after the document has loaded should indeed open a new document overwriting the current page. – Bergi Sep 04 '13 at 16:32

2 Answers2

1

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?

Firefox will:

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.

nmaier
  • 32,336
  • 5
  • 63
  • 78
  • This answer gave me further insight why I have experienced the differences between the browsers. A limited workaround is possible in Firefox that if the document.write is executed from the page body before the page rendering completes then the anchors are also not navigating in Firefox. If the document.write happens from an event handler after page load (like in my code example above) then document.write opens a new document in Firefox. – phantasm Sep 09 '13 at 11:08
0

Hi ran a few tests using ie8 ie9 and chrome, then did a bit of research. When your building your website and test it locally the javascript or in your case script wont always run on ie8 or ie9 because your active x controls are turnd off, but if you were to upload your page onto a server and then test it you will see that the document.write will indeed work online. I tested the following online and then ofline and it workd online

      <head>
    <title>Untitled Page</title>
<script type="text/javascript">
    function myFunction()
{

document.write("text <a href=\"#a\">link</a>");
document.close();
}
   </script> 
</head>
<body>
<label onclick='document.write("text <a href=\"#a\">link</a>");document.close();'>click</label>
<a onclick="myFunction()">click</a>
</body>     

as you can see its the same code just used diffrently but thats not the point. To debug properly and let the scripts work do activate your active x controls as follows For windows xp go to control panel then internet options. On the Tools menu, click Internet Options, and then click the Security tab. Click the Internet zone. Click Custom Level. In the Security Settings – Internet Zone dialog box, click Enable for Active Scripting in the Scripting section. heres a link that wil help you with a veriaty of browsers to let them run script locally. http://www.enable-javascript.com/

Nikki
  • 85
  • 16
  • I had no problem running the code in any of the browser you have tested (IE8,IE9,Chrome was all identical). The only differently behaving browser was **Firefox**. – phantasm Sep 08 '13 at 23:15
  • the person used an alternative way instead of document.write this link could mayb help out abit aswell http://stackoverflow.com/questions/4393288/why-does-javascript-document-write-not-work-on-firefox – Nikki Sep 10 '13 at 05:02
  • – Nikki Sep 10 '13 at 05:04
  • Hi again the following article explains a simpler alternative to using document.write aswell. http://www.sitepoint.com/insert-in-place-without-documentwrite/ – Nikki Sep 10 '13 at 05:28