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

Call Javasript Function

progdoctor
P: 13
I have a php web based application with dhtml menu.
Each time user click menu Item, the page loads external php page into a div container in that page and then load external javascript as the clientside event handler of that loaded page.I load both of them using ajax method.

The problem is: how to call a function of that external javascript soon after the external javascript file loaded into main page?

I tried eval(functionName()) but it returns error Object Required.I got confused because the functionName is declared in that external javascript.I tried to catch the readyState of XmlHttpRequest and the result shows that by the time i call functionName() XmlHttpRequest readyState is still 1.So then i puut a loop 'while do' that say when the readyState reach 4,call functionName() n then i got this Browser Message that say something like script make browser run slowly and nothing happened not even close to my expectation...

Please help me.. feel desperate now..
Jul 17 '08 #1
Share this Question
Share on Google+
7 Replies


hsriat
Expert 100+
P: 1,654
The external function should be considered as a part of the page (technically window) as soon as readystate changes to 4.
So instead of using any do while loop or using any eval, just call the function at the end of your onreadystatechange function. That may solve your problem.
Jul 17 '08 #2

progdoctor
P: 13
Hei thanku for your reply...

I have tried that one too.. but it still return object required message...
heres what code looks like

lets just say my main page is Main.php
and my child page is Child.php
my child exsternal js file is Child.php.js
function i would like to call in external js file is child_Load()

Expand|Select|Wrap|Line Numbers
  1. if(xml.readyState==4 && xml.status==200)
  2. {
  3.    includeExternJScript('contentScript','Child.php.js',xml.responseText);
  4.    child_Load(); //this return object required message error
  5.  
  6. }
  7.  
  8. function includeExternJScript(divId,scriptUrl,xmlResponse)
  9. {
  10.     get object 'head' tag
  11.     create element script;
  12.     set element script.text = xmlResponse;
  13.     add element script to object head as child 
  14. }
Jul 17 '08 #3

hsriat
Expert 100+
P: 1,654
Expand|Select|Wrap|Line Numbers
  1. function includeExternJScript(scriptUrl)
  2. {
  3.     var js=document.createElement('script')
  4.     js.type = "text/javascript";
  5.     js.src = scriptUrl;
  6.     document.getElementsByTagName("head")[0].appendChild(js);
  7. }
I think the function includeExternJScript() should be like this. What would xmlResponse has to do with the script?

Once you run this function, the external script would take time to be fetched from the server. So it would again need some time before you call your function.

So for that, either instead of calling load_child() in this script, call it at the end (append load_child(); to the last line) of that dynamically loaded js.

Or you should wait until the js is actually loaded.
For that, create a function like this:
Expand|Select|Wrap|Line Numbers
  1. do_child_Load() {
  2.     if (window.child_load)
  3.     child_load();
  4.     else setTimeout("do_child_Load()", 100);
  5. }
and call do_child_load() instead of child_load().

But anyhow, the first thing is recommended as the second may not work in some cases.
Jul 17 '08 #4

progdoctor
P: 13
Expand|Select|Wrap|Line Numbers
  1. function includeExternJScript(scriptUrl)
  2. {
  3.     var js=document.createElement('script')
  4.     js.type = "text/javascript";
  5.     js.src = scriptUrl;
  6.     document.getElementsByTagName("head")[0].appendChild(js);
  7.     js=document.createElement('script')
  8.     js.text = "child_Load()";    
  9.     document.getElementByTagName("head")[0].appendChild(js);
  10. }
  11.  
Is that how i should write the code?

I will try that

Anyway thanks
Jul 17 '08 #5

hsriat
Expert 100+
P: 1,654
No... not like this, but in your external js file, append this line in the end.
Expand|Select|Wrap|Line Numbers
  1. load_child();
Jul 17 '08 #6

progdoctor
P: 13
thank u for your help.. finally i got what i need.. here's the code ive been modified

Expand|Select|Wrap|Line Numbers
  1. function XMenu_OnClick(itemId,itemValue)
  2. {
  3.     var obj;
  4.     var url;
  5.  
  6.     for(var i=0;i<oXMenu._menuItem.length;i++)
  7.     {
  8.         if(oXMenu._menuItem[i]._id==itemId && oXMenu._menuItem[i]._isinduk==0)
  9.         {    
  10.             contentDiv.innerHTML = '';
  11.             removeExternJScript('contentScript');
  12.  
  13.             url = oXMenu._menuItem[i]._direktori+'/'+oXMenu._menuItem[i]._nmform;
  14.             scripturl =  "../Resources/Js/"+oXMenu._menuItem[i]._direktori+"/"+oXMenu._menuItem[i]._formpage+".js";
  15.  
  16.             obj = new Object();
  17.             obj.contentid='content';
  18.             obj.url = url;
  19.             oXAjaxLoader = new XAjaxLoader(obj);
  20.             oXAjaxLoader.showResult();
  21.             includeExternJScript(scripturl);
  22.             ToolbarState_Content();
  23.  
  24.             break;
  25.         }
  26.     }
  27. }
  28.  
  29. function includeExternJScript(scriptUrl)
  30. {  
  31.     var oHead = document.getElementsByTagName('head').item(0);
  32.     var oScript = document.createElement('script');
  33.     oScript.language = "javascript";
  34.     oScript.type = "text/javascript";
  35.     oScript.id = 'contentScript';
  36.     oScript.src = scriptUrl;
  37.  
  38.     oHead.appendChild(oScript); 
  39.         callChildInit();
  40. }
  41.  
  42. function removeExternJScript(sId)
  43. {
  44.     var oHead = document.getElementsByTagName('head').item(0);
  45.     var oScript = document.getElementById(sId);
  46.  
  47.     if(oScript!=null)
  48.     {
  49.         oScript.src='';
  50.         oScript.text='';
  51.         oHead.removeChild(oScript);
  52.     }    
  53. }
  54.  
  55. function callChildInit()
  56. {
  57.     if(oXAjaxLoader.oXml.readyState==4)
  58.         Page_Load();
  59.     else
  60.         setTimeout("callChildInit()",100);
  61. }
Jul 18 '08 #7

hsriat
Expert 100+
P: 1,654
That's good you got it working. :)
Jul 18 '08 #8

Post your reply

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