SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

  2. #2
    SitePoint Enthusiast David.A's Avatar
    Join Date
    May 2010
    Location
    Milky Way Galaxy
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doesn't seem to work, the script isn't run, even though it's in the DOM with either inner HTML:
    HTML Code:
    <!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:

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

  4. #4
    SitePoint Enthusiast David.A's Avatar
    Join Date
    May 2010
    Location
    Milky Way Galaxy
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is this?

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

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(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.

  6. #6
    SitePoint Enthusiast David.A's Avatar
    Join Date
    May 2010
    Location
    Milky Way Galaxy
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,868
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by David.A View Post
    What is this?

    Code:
    document.getElementById('ad_top').innerHTML = '<scr'+'ipt>document.writeln("test");</scr'+'ipt>';
    Try:
    Code:
    document.getElementById('ad_top').innerHTML = '<script>' + document.writeln("test") + '</script>';
    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:

    Code:
    document.getElementById('ad_top').innerHTML = "test";
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •