Firefox behaves differently than other browsers if links that were created using document.write are linked to the page. Is there a workaround?

Download 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. This will overwrite the page with document.write, which will contain a binding called link.

In IE8, IE9, Chrome, the last time this link is clicked will not load the page.

In Firefox (tested with the latter and FF6), clicking the link reloads the original page.

Firefox's behavior seems to be wrong, since using anchors should not result in pages loading. If the document is not used, clicking on the anchors will not load the page even in Firefox.

Is there a workaround for this?

The goal would be to use document.write. This example simply mimics the fact that we would like to download another full web page, including a lot of javascript code with AJAX, which should run correctly after inclusion.

+4
source share
2 answers

The spec for write() somewhat unclear in this regard:

Enter a line of text into the document stream opened by the open () function.

Unclear being: what should happen if the stream is not already open / larger?

Firefox will :

Writing to a document that is already loaded without calling document.open () automatically makes a call to document.open.

Since this effectively creates (reads: "loading") a new document, therefore, the page is navigated and loaded. There is no way around this in Firefox. Of course, you can point out a bug and request Chrome / IE parity for the sake of an open website.

+1
source

Hi conducted some tests using ie8 ie9 and chrome, and then did some research. When you build your site and test it locally, javascript or in your case the script usually does not run on ie8 or ie9 because your active x controls are disabled, but if you have to upload your page to the server, then check it will see that document.write will really work on the Internet. I tested the following online and then online, and it worked 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> 

since you can see that the same code is used just differently, but it is not. To properly debug and let scripts work, activate the active x controls as shown below. For Windows XP, go to Control Panel, then select Internet. From the Tools menu, select Internet Options, and then click the Security tab. Click on the Internet zone. Click "Custom Level." In the Security Settings - Internet Zone dialog box, click Enable for Active Scripting in the Scripting section. this is a link that will help you use a browser to let them run the script locally. http://www.enable-javascript.com/

0
source

Source: https://habr.com/ru/post/1500568/


All Articles