Unusual behavior in function calls in java script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rakesh19
    New Member
    • Aug 2008
    • 17

    #1

    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.
    [CODE=javascript]
    headTag.appendC hild(imageScrip tTag);
    includeJSFiles( 'js/image-slideshow.js',' text/javascript');
    includeJSFiles( 'OpenLayers-2.6/lib/OpenLayers.js', 'text/javascript');
    includeJSFiles( 'OpenLayers-2.6/lib/Firebug/firebug.js','te xt/javascript');
    includeJSFiles( "js/mapStandaloneBo x.js","text/javascript");
    includeJSFiles( 'js/dom-drag.js','text/javascript');
    init();
    document.getEle mentById('GoVid ').onclick=chec kButtonCall;
    checkButtonCall ();
    }
    }
    }
    function includeJSFiles( src,type) {
    var headTag=documen t.getElementsBy TagName('head')[0];
    var scriptTag=docum ent.createEleme nt('script');
    scriptTag.src=s rc;
    scriptTag.type= type;
    headTag.appendC hild(scriptTag) ;
    return true;
    //console.log(hea dTag.innerHTML) ;
    }
    [/CODE]
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

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

    kind regards

    Comment

    • rakesh19
      New Member
      • Aug 2008
      • 17

      #3
      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.

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #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 ...

        [CODE=javascript]if (typeof init != 'undefined') {
        init();
        }[/CODE]
        kind regards

        Comment

        • rakesh19
          New Member
          • Aug 2008
          • 17

          #5
          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 ...

          [CODE=javascript]if (typeof init != 'undefined') {
          init();
          }[/CODE]
          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.

          Comment

          Working...