How to append script to head of page?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bugboy
    New Member
    • Sep 2007
    • 160

    #1

    How to append script to head of page?

    I'm retrieving list data via ajax then appending a retrieved script 'str[x]' to the page. The script is suppose to create a new 'List' object with the retrieved data.

    Code:
    var head = document.getElementsByTagName("head")[0];
    var script = document.createElement("script");
    script.type="text/javascript";
    script.appendChild(document.createTextNode(str[x])); 
    head.appendChild(script);
    Then i'm trying to call a method of the new object. It works if i place an alert before i call the method, but does not work when the alert is removed. At first i assumed the alert was creating a delay so all it needed was a timeout.. but adding that didn't work either.

    Any ideas why i can access the object after an alert but not without it? I'm very confused..

    Thanks!


    Works:
    Code:
    function Person(lists){
    	this.list=lists.split(",");
    	get_list(lists); 
    	alert('ok');//<----- works with this here
    	listobj[this.list[0]].renderList('target1');
    	listobj[this.list[1]].renderList('target2');
    	}
    Does not work:
    Code:
    function Person(lists){
    	this.list=lists.split(",");
    	get_list(lists); 
    	listobj[this.list[0]].renderList('target1');
    	listobj[this.list[1]].renderList('target2');
    	}
    Update: the only thing i can think of is that there is a problem creating a obj while creating an obj. Person is an object and get_lists creates several objects before during the Person object creation... but i guess that doesn't explain why adding the alert in there makes it suddenly work..
    Last edited by bugboy; Nov 15 '10, 06:17 PM. Reason: no response
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    Code:
    get_list(lists);
    does in fact an ajax-call?

    Comment

    • bugboy
      New Member
      • Sep 2007
      • 160

      #3
      Yes this is an ajax call that appends the results to the head of the page.

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        in that case the line
        Code:
        listobj[this.list[0]].renderList('target1');
        cannot rely on the response ... since the response is reliably ready when the request is finished - your first assumption is correct - you would need to process the response in the request's final callback - the timeout might even have been to short ...

        Comment

        • bugboy
          New Member
          • Sep 2007
          • 160

          #5
          Thanks gits, I set a 10 sec timeout which is much longer than i took to click an alert box and it still didn't work. I've move 'listobj' call to another point in the code and it now works.

          Thanks for your help!

          Comment

          Working...