Running write() after page load?

I have a site that has 3 ads from an ad network inserted using document.write(‘<scr’+'ipt… but the ads are huge flash ad and the client is complaining the site is loading slow (because the ads are loading while the pages is rendering or sometimes not loading). Is there anyway to offset the script from executing until after load? I’ve tried to div.innerHTML = '<script src="… after the page has loaded, but that doesn’t work.

The only other thing I can think of is the place the ads at the end of the HTML and try and place them using absolute position to where they should be on the page (only will work with 2 of the ads)

Thanks.

Oh, and the result of the script that is written using write(), then writes the ad by using writeln(), so I can’ even do something like:

s = document.createElement(‘script’);
s.src = …;
document.getElementById(‘ad_top’).appendChild(s);

as it doesn’t do anything.

Well, it’s as close as you’ll get with that method. It works fine for me except I also get an undefined in there.

But you don’t need to use that anyways! You’re writting just what you want inside of it anyways. And should you need a variable of some sort, you wouldn’t use writeLn anyways.

Doesn’t seem to work, the script isn’t run, even though it’s in the DOM with either inner HTML:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>Test</title>
	</head>
	<body>
		<div id="ad_top"></div>
<script type="text/javascript">
	window.onload = function() {
		document.getElementById('ad_top').innerHTML = '<scr'+'ipt>document.writeln("test");</scr'+'ipt>';
	}
</script>
	</body>
</html>


or createElement:


s = document.createElement('script');
s.type = 'text/javascript';
s.innerHTML = 'document.writeln("test");'
document.getElementById('ad_top').appendChild(s);


That’s not how document.writeln works. It writes directly to the page when run, it doesn’t return anything to concatenate with the string.

You can’t use </ inside of JavaScript since that is taken to be the end of script marker.

document.write and document.writeln are obsolete and shouldn’t be used at all.

In any case to achieve what you want with that particular code you just need:

document.getElementById('ad_top').innerHTML = "test";

Using innerHTML should work perfectly. Your problem is probably incorrect usage of quotes; where you combine ’ and " thus it ends before it should.

If that isn’t it, then make sure div is pointing to the correct spot, and has been fully created before you use innerHTML.

What is this?

document.getElementById('ad_top').innerHTML = '<scr'+'ipt>document.writeln("test");</scr'+'ipt>';

Try:


document.getElementById('ad_top').innerHTML = '<script>' + document.writeln("test") + '</script>';