Monday, July 13, 2009

How to Reference a Javascript Function in One Frame from Another Frame?

Okay, I've already got this one coded for a single page, but the next part of the project requires that I split it up into frames (ancient, FRAMESET coding frames, I know...)





Anyway, the purpose is to create a list box that changes a picture depending on what selection is in the list box. I already have the code finished and working, but I just need to know what code you would use, and where you would place it to make a listbox from one HTM file reference a function in another HTM file.

How to Reference a Javascript Function in One Frame from Another Frame?
Try


window.parent.%26lt;name of the frame%26gt;.functionName();





Here is sample code.


top.htm (The html file containing frameset)





%26lt;html%26gt;


%26lt;frameset rows="250,*"%26gt;


%26lt;frame id="fm1" name="fm1" src="fm1.htm"%26gt;%26lt;/frame%26gt;


%26lt;frame id="fm2" name="fm2" src="fm2.htm"%26gt;%26lt;/frame%26gt;


%26lt;/frameset%26gt;


%26lt;/html%26gt;





File fm1.htm:


%26lt;html%26gt;


%26lt;head%26gt;


%26lt;script language="JavaScript"%26gt;


function fmf1() {


alert ("This is frame 1");


}


%26lt;/script%26gt;


%26lt;body%26gt;


This is frame 1


%26lt;/body%26gt;


%26lt;/html%26gt;





File fm2.htm





%26lt;html%26gt;%26lt;body


onLoad="window.parent.fm1.fmf1();"%26gt;


This is frame 2


%26lt;/body%26gt;


%26lt;/html%26gt;





Yahoo Answers is not showing the onLoad line correctly. Replace three dots with an opening brace "(", a closing brace ")" and a semicolon ";".





Hope it helps.


No comments:

Post a Comment