Include the code in both frames. Then call the function in another frame. Suppose there is a top-level global function called doWork() .
You enter the button in frame2, and you want to execute the function in frame1 (where frame1 is the window object representing frame1). Assuming that they are in the same domain and pass tests with the same origin , you can simply go to frame1 and call doWork() in this frame with:
frame1.doWork();
This works because all global javascript global variables and functions are window object properties for this browser window / frame. Each window / frame has its own set of top-level properties. If you are in the same domain and can get the corresponding window object, you can simply call javascript in that window. Javascript must be present in this window. You cannot run javascript from your window in the contents of another window, but if you put the code in both windows, you can execute it in any of the windows, starting with the right window object.
How you get the right window object for a particular frame depends on how your frames are structured.
Assuming again that these frames are in the same domain and pass the usual tests with the same origin , you can also write the javascript function in frame2, which works with the contents of frame1. For example, you can get this code in frame2:
frame1.document.getElementById("foo").value = "";
So you can write a function in frame2 that will work with frame1. However, you cannot just execute a function from frame2 in the contents of frame1 without writing it to know about working in another frame.
For example, you can write a function that will work with the desired window:
function clearValue(id, win) { win.document.getElementById(id).value = ""; }
source share