Call Javasript Function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • progdoctor
    New Member
    • Jul 2008
    • 13

    Call Javasript Function

    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(functionNa me()) 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..
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    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 onreadystatecha nge function. That may solve your problem.

    Comment

    • progdoctor
      New Member
      • Jul 2008
      • 13

      #3
      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()

      Code:
      if(xml.readyState==4 && xml.status==200)
      {
         includeExternJScript('contentScript','Child.php.js',xml.responseText);
         child_Load(); //this return object required message error
        
      }
       
      function includeExternJScript(divId,scriptUrl,xmlResponse)
      {
          get object 'head' tag
          create element script;
          set element script.text = xmlResponse;
          add element script to object head as child 
      }
      Last edited by acoder; Jul 17 '08, 11:17 AM. Reason: Added [code] tags

      Comment

      • hsriat
        Recognized Expert Top Contributor
        • Jan 2008
        • 1653

        #4
        [CODE=javascript]function includeExternJS cript(scriptUrl )
        {
        var js=document.cre ateElement('scr ipt')
        js.type = "text/javascript";
        js.src = scriptUrl;
        document.getEle mentsByTagName( "head")[0].appendChild(js );
        }[/CODE]
        I think the function includeExternJS cript() 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:[CODE=javascript]do_child_Load() {
        if (window.child_l oad)
        child_load();
        else setTimeout("do_ child_Load()", 100);
        }[/CODE]

        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.

        Comment

        • progdoctor
          New Member
          • Jul 2008
          • 13

          #5
          Code:
          function includeExternJScript(scriptUrl)
          {
              var js=document.createElement('script')
              js.type = "text/javascript";
              js.src = scriptUrl;
              document.getElementsByTagName("head")[0].appendChild(js);
              js=document.createElement('script')
              js.text = "child_Load()";    
              document.getElementByTagName("head")[0].appendChild(js);
          }
          Is that how i should write the code?

          I will try that

          Anyway thanks
          Last edited by acoder; Jul 17 '08, 11:19 AM. Reason: Added [code] tags

          Comment

          • hsriat
            Recognized Expert Top Contributor
            • Jan 2008
            • 1653

            #6
            No... not like this, but in your external js file, append this line in the end.
            Code:
            load_child();

            Comment

            • progdoctor
              New Member
              • Jul 2008
              • 13

              #7
              thank u for your help.. finally i got what i need.. here's the code ive been modified

              Code:
              function XMenu_OnClick(itemId,itemValue)
              {
              	var obj;
              	var url;
              
              	for(var i=0;i<oXMenu._menuItem.length;i++)
              	{
              		if(oXMenu._menuItem[i]._id==itemId && oXMenu._menuItem[i]._isinduk==0)
              		{	
              			contentDiv.innerHTML = '';
              			removeExternJScript('contentScript');
              			
              			url = oXMenu._menuItem[i]._direktori+'/'+oXMenu._menuItem[i]._nmform;
              			scripturl =  "../Resources/Js/"+oXMenu._menuItem[i]._direktori+"/"+oXMenu._menuItem[i]._formpage+".js";
              			
              			obj = new Object();
              			obj.contentid='content';
              			obj.url = url;
              			oXAjaxLoader = new XAjaxLoader(obj);
              			oXAjaxLoader.showResult();
              			includeExternJScript(scripturl);
              			ToolbarState_Content();
              			
              			break;
              		}
              	}
              }
              
              function includeExternJScript(scriptUrl)
              {  
              	var oHead = document.getElementsByTagName('head').item(0);
              	var oScript = document.createElement('script');
              	oScript.language = "javascript";
              	oScript.type = "text/javascript";
              	oScript.id = 'contentScript';
              	oScript.src = scriptUrl;
              	
              	oHead.appendChild(oScript); 
                  	callChildInit();
              }
              
              function removeExternJScript(sId)
              {
              	var oHead = document.getElementsByTagName('head').item(0);
              	var oScript = document.getElementById(sId);
              
              	if(oScript!=null)
              	{
              		oScript.src='';
              		oScript.text='';
              		oHead.removeChild(oScript);
              	}	
              }
              
              function callChildInit()
              {
              	if(oXAjaxLoader.oXml.readyState==4)
              		Page_Load();
              	else
              		setTimeout("callChildInit()",100);
              }

              Comment

              • hsriat
                Recognized Expert Top Contributor
                • Jan 2008
                • 1653

                #8
                That's good you got it working. :)

                Comment

                Working...