469,575 Members | 1,658 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,575 developers. It's quick & easy.

Unusual behavior in function calls in java script

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
4 1910
gits
5,390 Expert Mod 4TB
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
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
5,390 Expert Mod 4TB
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
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.

Similar topics

4 posts views Thread by DaKoadMunky | last post: by
3 posts views Thread by Farooq Khan | last post: by
6 posts views Thread by Kenneth | last post: by
4 posts views Thread by the.new.delboy | last post: by
reply views Thread by Michal Kwiatkowski | last post: by
4 posts views Thread by Newbie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.