Forum Moderators: open

Message Too Old, No Replies

Calling a function from a dynamically loaded js file?

         

NooK

7:14 am on Sep 7, 2007 (gmt 0)

10+ Year Member



I am trying to dynamically load a js file and call a function from it.

<SCRIPT type="text/javascript">
ajaxManager('load_js','treemenu.js');
createTreeMenu(itemID');
</SCRIPT>

I try the code above but when I just enter the webpage it says it cannot find the function createTreeMenu (Which is inside the js file), but as soon as I refresh the page it works.

I have tried various combinations of calling those functions on load or javascript on the document body but none have given any results.

Any ideas?

Best Regards

NooK

mehh

2:52 pm on Sep 8, 2007 (gmt 0)

10+ Year Member



you are calling the function before the JS file loads. put the function at the end of you js file or do it onload

daveVk

12:15 am on Sep 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Two conditions need checking for
1 - page in loaded, including 'itemId'
2 - js in loaded
I would not assume the order in which they occur, so use a timeout loop on page load with "if (createTreeMenu)" test, or you can add code on js loaded them on both events check if other has occured and call function or flag that event has occured.

NooK

6:16 am on Sep 10, 2007 (gmt 0)

10+ Year Member



Thank you. I have, as I pointed out, tried to call the createTreeMenu function onload and at the end of the body but none had any success. I have fixed it by adding a third parameter to the AJAX functin and just calling eval() on that parameter, not sure if the best solution but it seems to work fine thanks.

Best Regards

NooK