Connecting Tech Pros Worldwide Forums | Help | Site Map

Unusual behavior in function calls in java script

Newbie
 
Join Date: Aug 2008
Posts: 17
#1: Nov 20 '08
Hi,
I am dynamically including .js files on a button click. There is a segregated function to do the same. It doesnt seems to work well. If I put a alert tag in the includeJSFile function, it works well. Else it says init is not defined. Is there peculiarity in how function calls are terminated or something
I am attaching the code snippet below.
Expand|Select|Wrap|Line Numbers
  1. headTag.appendChild(imageScriptTag);         
  2.     includeJSFiles('js/image-slideshow.js','text/javascript');
  3.     includeJSFiles('OpenLayers-2.6/lib/OpenLayers.js','text/javascript');
  4.     includeJSFiles('OpenLayers-2.6/lib/Firebug/firebug.js','text/javascript');
  5.     includeJSFiles("js/mapStandaloneBox.js","text/javascript");    
  6.     includeJSFiles('js/dom-drag.js','text/javascript');    
  7.     init();
  8.     document.getElementById('GoVid').onclick=checkButtonCall;
  9.     checkButtonCall();
  10.     }
  11. }
  12. }
  13. function includeJSFiles(src,type) {
  14.     var headTag=document.getElementsByTagName('head')[0];
  15.     var scriptTag=document.createElement('script');    
  16.     scriptTag.src=src;
  17.     scriptTag.type=type;
  18.     headTag.appendChild(scriptTag);
  19.     return true;
  20.     //console.log(headTag.innerHTML);
  21. }
  22.  

gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,134
#2: Nov 20 '08

re: Unusual behavior in function calls in java script


what does the init() method do? is it a function that relies on one of the included scripts before?

kind regards
Newbie
 
Join Date: Aug 2008
Posts: 17
#3: Nov 20 '08

re: Unusual behavior in function calls in java script


Quote:

Originally Posted by gits

what does the init() method do? is it a function that relies on one of the included scripts before?

kind regards

Thanks for reply

yep. init() relies on the openlayers script included above. How do I invoke Openlayers constructor. Including in head tag and loading seems to invoke that constructor but not the tag addition by later method.
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,134
#4: Nov 20 '08

re: Unusual behavior in function calls in java script


i think the include of scripts that you do here is just an async process ... so you cannot rely on the code instantly. since you add the src-attrib this might start to load the script when you add it to the document and the init() method is called to early. you could poll and wait for all code that you want to use during the process ... you may use an interval and ask for something like ...

Expand|Select|Wrap|Line Numbers
  1. if (typeof init != 'undefined') {
  2.     init();
  3. }
kind regards
Newbie
 
Join Date: Aug 2008
Posts: 17
#5: Nov 20 '08

re: Unusual behavior in function calls in java script


Quote:

Originally Posted by gits

i think the include of scripts that you do here is just an async process ... so you cannot rely on the code instantly. since you add the src-attrib this might start to load the script when you add it to the document and the init() method is called to early. you could poll and wait for all code that you want to use during the process ... you may use an interval and ask for something like ...

Expand|Select|Wrap|Line Numbers
  1. if (typeof init != 'undefined') {
  2.     init();
  3. }
kind regards

yep. that sounds good. Actually, openlayers inturn loads a many other js files. Since its a async process, that happens separately and init has few components of those js file, hence it breaks. Thanks.
Reply