SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Calling an external script by creating a script object in the DOM

    My site has a widget that runs by calling an external script on a server outside my control. Normally this runs within a div as the page loads and the widget is displayed within the div.

    What I am trying to do is call the script AFTER the page loads rather than while it loads. To do this, I am adding the script to the DOM (in the div I want, not to the head) on window.onload. An alert shows that the script is being created in the div.

    The problem is that the script does not appear to fire in IE6 and IE7. In Firefox it runs, but rather than adding the widget to the div, it creates a new page (everything else on the page is lost).

    I have made a test page to show the problem, as well as another page showing how the widget normally loads when the called while the page is loading.

    This is the code I am using (added to the head of the html file):

    Code JavaScript:
    <script type="text/javascript">	
    	window.onload = function() {
    		var scratchBlogcatalog = document.getElementById("scratch_blogcatalog");
    		var scratchBlogcatalogScript = document.createElement('script');
    		scratchBlogcatalogScript.id = 'scratchBlogcatalogScript';
    		scratchBlogcatalogScript.type = 'text/javascript';
    		scratchBlogcatalogScript.src = "http://pub.mybloglog.com/comm2.php?mblID=2007080509201853&c_width=189&c_sn_opt=n&c_rows=3&c_img_size=f&c_heading_text=MyBlogLog+Readers&c_color_heading_bg=336699&c_color_heading=ffffff&c_color_link_bg=E3E3E3&c_color_link=336699&c_color_bottom_bg=336699";
    		scratchBlogcatalog.appendChild(scratchBlogcatalogScript);
    		alert(scratchBlogcatalog.innerHTML);
    	}
    </script>

    I've run out of ideas on what to try next. I'd be grateful for any help. Thanks.

  2. #2
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I got the answer

    I got the answer to this elsewhere, but in case anyone else is interested, I'll put it here.

    It seems the mybloglog.com script uses document.write() which can only be used while the page is loading. It can't be used in a script run after the page has finished loading.


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
  •