views:

27

answers:

4

basically i have a blank html page that includes a javascript file, and in the javascript file i have this:

function doIt() {
 document.writeln("asdf");
}

// could also be setTimeout
setInterval("doIt()", 5000);

When the html page loads, it waits 5 seconds and then will output "asdf" every 5 seconds to the screen. If i hit refresh or f5, nothing happens. When i view the source, the page is blank. Is there any reason why when i view source i dont even see the:

<script type="text/javascript" src="test.js"></script>

on the html page? Im assuming i cant refresh the page because of the blank source. Any ways to resolve this?

Thanks!

+1  A: 

You cannot use document.write like this after the page finishes loading.

Instead, you should append to the text of a DOM element.

SLaks
+1  A: 

When the html page loads, it waits 5 seconds and then will output "asdf" every 5 seconds to the screen.

When the page has finished loading, the document is closed and can't be written to. So if you call document.write() after this point, it assumes you mean to call document.open() and completely replace the page. The old document is unloaded to be replaced by the new one you're about to write, which is why you can't view the source of the old page any more.

And because you don't call document.close(), the throbber will keep spinning, waiting for the new document to be completed (which will never happen).

As SLaks said, you should interact with the DOM instead of using document.write() which is generally best avoided (except in a few specific cases to do with writing initial documents to popups or iframes).

bobince
A: 

thanks guys. i was able to see what you guys meant by having to modify the DOM to add in javascript after the page has loaded. I was able to do that but ran into the same issue:

basically i needed to use setInterval to do some polling to wait for data to be sent to this iframe from the parent, once it received the data, then it would output some javascript (which i created a new script element in the DOM for). but the problem is that the src of that element is pointing to an external url from which im getting some data from, and the way that external source returns the data is by using document.write - which i have no control over.

is there any other option for doing the polling besides using setInterval? i'd like to continue polling until a max timeout, and then if the max timeout is reached, then execute the rest of the page, then i wont have to worry about the document.write issue from the external source.

is another option to make that call to the external source and parse the response and create the dom elements with my javascript?

czer
A: 

Just an idea, didn't have chance to test it.

Have your script create blank window and inject script into there to call the external script:

var oWindow = window.open("about:blank", "dummy", "width=0,height=0");
oWindow.document.write("<html>");
oWindow.document.write("<body>");
oWindow.document.write("<" + "script" + " type=\"text/javascript\" src=\"external.js\"></" + "script>");
oWindow.document.write("</body>");
oWindow.document.write("</html>");
oWindow.document.close();

and then read the oWindow.document.body.innerHTML to see the "output" of the external script.

Shadow Wizard