473,385 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to append script to head of page?

bugboy
160 100+
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.

Expand|Select|Wrap|Line Numbers
  1. var head = document.getElementsByTagName("head")[0];
  2. var script = document.createElement("script");
  3. script.type="text/javascript";
  4. script.appendChild(document.createTextNode(str[x])); 
  5. 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:
Expand|Select|Wrap|Line Numbers
  1. function Person(lists){
  2.     this.list=lists.split(",");
  3.     get_list(lists); 
  4.     alert('ok');//<----- works with this here
  5.     listobj[this.list[0]].renderList('target1');
  6.     listobj[this.list[1]].renderList('target2');
  7.     }
Does not work:
Expand|Select|Wrap|Line Numbers
  1. function Person(lists){
  2.     this.list=lists.split(",");
  3.     get_list(lists); 
  4.     listobj[this.list[0]].renderList('target1');
  5.     listobj[this.list[1]].renderList('target2');
  6.     }
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..
Nov 14 '10 #1

✓ answered by gits

in that case the line
Expand|Select|Wrap|Line Numbers
  1. 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 ...

4 3054
gits
5,390 Expert Mod 4TB
Expand|Select|Wrap|Line Numbers
  1. get_list(lists);
does in fact an ajax-call?
Nov 17 '10 #2
bugboy
160 100+
Yes this is an ajax call that appends the results to the head of the page.
Nov 17 '10 #3
gits
5,390 Expert Mod 4TB
in that case the line
Expand|Select|Wrap|Line Numbers
  1. 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 ...
Nov 17 '10 #4
bugboy
160 100+
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!
Nov 18 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: pantagruel | last post by:
The following does not work in firefox: <script defer="defer"> var x=document.getElementsByName("repositoryNamespace") alert(x.length + " elements!")
2
by: mmcc128 | last post by:
When I call this... ------------------------- function funcWinTopJS(){ var s = document.createElement('script'); s.type = "text/javascript"; s.src = "Javascript/Master.asp";...
1
by: Prad Bok via .NET 247 | last post by:
(Type your message here) -------------------------------- From: Prad Bok I have a web page that takes long time(more than 3 minutes) tofinish. I am running IIS6 on windows 2003 server. I...
3
by: Raymond | last post by:
Hello folks, My script is as below: parent.menu.location.reload(true); parent.frames("menu").TreeView1.CollapseAll(); parent.frames("menu").TreeView1.SelectNodeById(id);...
0
by: aBs0lut30 | last post by:
Hi Guys, I am working on a site and recently added several client callbacks to the project. Everything works GREAT while debugging the site in visual studio. Last night I copied the site up to our...
5
by: CoolForU | last post by:
this is my code and i call this code from code behinde file on page load like this useragree.Attributes.Add("onclick", "CheckSelection();") this is working fine with IE but not working with...
1
by: pandharinath | last post by:
I want to display fields(row) selected from grid in popup window to textboxes in Opener window. I have written code as follows : Main Opener window protected void Button1_Click(object...
4
by: MIkersson | last post by:
Hello, I have a framset with 3 frames: HEADER, CONTENT and FOOTER how can I get a "nextpage script" for use in the "header" and open links in "content" frame?. first page | Next page | Next...
7
by: khanishfaq82 | last post by:
This Script below works perfectly in firefox but fails to execute in Internet Explorer.Kindly copy and paste the script below to see it working.What i require is if i click 'MENU1' and it is still in...
3
by: mark roldan | last post by:
Hi, this is a code for form verification... I added the function results() then the entire script is now not working. Need help... *note that if I omit the results() function, other functional parts...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.