473,750 Members | 2,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Call Javasript Function

progdoctor
13 New Member
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..
Jul 17 '08 #1
7 2261
hsriat
1,654 Recognized Expert Top Contributor
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.
Jul 17 '08 #2
progdoctor
13 New Member
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
1,654 Recognized Expert Top Contributor
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 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:
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
13 New Member
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
1,654 Recognized Expert Top Contributor
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
13 New Member
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
1,654 Recognized Expert Top Contributor
That's good you got it working. :)
Jul 18 '08 #8

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

Similar topics

1
7100
by: Jim Mitchell | last post by:
It seems that I can just insert <href=# onclick:javascript:CallJava()>Go Java</a> into the text property of a label control to call a Javasript function. It seems to be a very useful tool. 1) Any reason not to do it? 2) Is there a better way? Thanks in advance.
1
2873
by: techy techno | last post by:
Hii Just wanted to know how can I decorate my texboxes and Listmenu which is called from a JS file using the following code below: document.write("<SELECT NAME='cur2' ONCHANGE='cconv1();'>"); document.write("<OPTION VALUE='0.345066110642241'>Argentina Peso </OPTION>"); document.write("<OPTION VALUE='0.790200069503053'>Australia Dollar
8
2803
by: Bartosz Wegrzyn | last post by:
Please look at my code. I do validation of my form before I submit it. I want to be able to to press one form button without running validating script. I just want to go directly to my php script. I tried to do this onclick="document.form1.submit()" but than the button value is not posted and my php script is not working.
2
1409
by: AshuPd | last post by:
Hi all !!! I am fighting with a very interesting issue... What i need is to link a javasript with a imagebutton control which is already on a datalist. the problem is, if i place the control out of the DL it works fine but the id of the control looses its declaration as it is placed at the DL. Pl. some one give some idea to solve it... TIA Ashutosh
1
1234
by: Yasantha | last post by:
hi, how to call a text box to javasript
18
2778
by: sandeepdesai | last post by:
Hi.. i am new to javascripts, i am using a addRow scipt to add rows to a table dynamically. one of the cells(say cell2) in a Nth row needs to be hyperlinked to another script. for example: function addRow() // the add row script {
2
1690
by: pedalpete | last post by:
Hi All, I'm using Bill Scott's YUI carousel v5.6, and I've run into a crazy looping javascript for some reason, and I can't seem to get around it. The carousel loads a bunch of items, 7 of which are visible, and then via ajax it loads more and ads them to them on to the list as it goes. If a user selects one of the items, that item scrolls to the middle and is highlighted. I've got all this working, except that when a user clicks,...
2
1514
bilibytes
by: bilibytes | last post by:
Hi, i have been creating an ajax aplication on my website and it worked as i expected it to work on Safari and Firefox, until i upgraded to Firefox 3. It doesn't work either on windows. the url is: http://www.whichselect.com I have created an event, that was triggered onmouseover the horizontal nav bar
4
2446
by: dupdupdup | last post by:
Hello there, im needed to develop a gallery for this website. im using hotspots to go to my frames in my flash Each hotspot goes to each frame. The flash is loaded properly. When i right click and click play it goes to the next frame. But the Hotspots just dont work with IE. For your information: This works with other browsers like firefox, safari and Opera. MY HEADER <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"...
0
9001
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9396
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9342
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8263
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4888
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3323
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.