×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Regular Contributor
    Join Date
    Apr 2003
    Location
    Akron Ohio USA
    Posts
    229

    json script tag hack does not work from window.onload

    I am experimenting with using the dynamic script tag hack to load json data as a javscript. This does not seem to work if I append the script tag from a function called from window.onload event. I get the same behavior in both the windows desktop PC version of Safari 3.1 and the Nokia S60 web browser.

    The following code works:
    Code:
    // function called from external json js file
    function bookdetails_onload(data){
    	alert("data loaded");
    }
    	
    // load the json data
    function loadJSON(url){
    	var scripttag = document.createElement("script"); // create the script tag
    	scripttag.src = url; // set the src attribute to the json file url
    	scripttag.type="text/javascript"; // set the mime type
    	document.getElementsByTagName("head")[0].appendChild(scripttag); // append the script tag to the HTML document, causing the json file to load
    }
    loadJSON("example.js");
    The following example does not seem to load any javascript.

    Code:
    // function called from external json js file
    function bookdetails_onload(data){
    	alert("data loaded");
    }
    	
    // load the json data
    function loadJSON(url){
    	var scripttag = document.createElement("script"); // create the script tag
    	scripttag.src = url; // set the src attribute to the json file url
    	scripttag.type="text/javascript"; // set the mime type
    	document.getElementsByTagName("head")[0].appendChild(scripttag); // append the script tag to the HTML document, causing the json file to load
    }
    
    function init(){
    	loadJSON("example.js"); // attempt to load the json file
    }
    
    window.onload = init;

  2. #2
    Registered User
    Join Date
    Mar 2008
    Posts
    47

    Re: json script tag hack does not work from window.onload

    Hi,

    Any change that you got this to work?

    best regards,
    Magnus

  3. #3
    Regular Contributor
    Join Date
    Apr 2003
    Location
    Akron Ohio USA
    Posts
    229

    Re: json script tag hack does not work from window.onload

    not yet, though I havent played with it for awhile. I would imagine there is some way to get this to work, but you can always use the eval() approach which seems to be work fine.

Similar Threads

  1. Replies: 14
    Last Post: 2008-04-14, 16:43
  2. Replies: 3
    Last Post: 2007-09-26, 15:41
  3. tag does not work...
    By dtmtraxx in forum Browsing and Mark-ups
    Replies: 0
    Last Post: 2002-08-23, 11:55

Posting Permissions

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