By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,230 Members | 2,471 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,230 IT Pros & Developers. It's quick & easy.

Unusual behavior in function calls in java script

P: 17
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.  
Nov 20 '08 #1
Share this Question
Share on Google+
4 Replies


gits
Expert Mod 5K+
P: 5,352
what does the init() method do? is it a function that relies on one of the included scripts before?

kind regards
Nov 20 '08 #2

P: 17
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.
Nov 20 '08 #3

gits
Expert Mod 5K+
P: 5,352
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
Nov 20 '08 #4

P: 17
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.
Nov 20 '08 #5

Post your reply

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