开发者

Javascript document.write weird behaviour in Firefox

When I write the following code directly into my html page under a s开发者_如何学Ccript tag, the string "test" gets appended to my page without replacing the rest of its content (1):

document.write("test");

However, if I place that same code into a separate javascript file like (2):

<script src="http://127.0.0.1/whatever.js" type="text/javascript"></script>

Suddenly that same code rewrites my entire page.

Is there a way to perform a document.write() from a remote file and get the result in (1)?

Thanks for your time.


If you use doc.write while the page page is rendering, it will insert or append the string. If you use doc.write after it's rendered, or after window.onload, it will essentially begin the rendering process again, and overwrite the page.

It's my guess that you are getting asynchronous behavior when loading the script, and it's not executing until after onload. I can't recreate your problem.


You may be including your script at the top of the page. Where it gets the document.write() stuff and thus writes the text instead of a append behaviour.


The safer solution is to append a document element to the page - that should always work

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜