/    Sign up×
Community /Pin to ProfileBookmark

Function’s scope

Can a function be defined in a browser window and the used in another winow?

Thanks
Jay

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@CharlesJan 20.2005 — Sort of. In JavaScript functions are really properties of some Object. If it the property of a different window then you will have to invoke it thusly:

[font=monospace]Window.function()[/font]
Copy linkTweet thisAlerts:
@7studJan 20.2005 — Can a function be defined in a browser window and the used in another winow?[/quote]
Yes. It's done all the time with frames and popup windows. You just have precede the function name with a reference to the other window. With popups, it's pretty straight forward. window.open() returns a reference to the popup, so javascript in the main page can do something like this:

main.htm
--------


var popup_win = window.open('somepage.htm', ....);

popup_win.function_in_popup_page();

Javascript in the popup page can reference functions in the main page like this:

popup

somepage.htm
------------


opener.function_in_main_page();

With frames the reference to the proper window depends on the frame structure. Typically, you will have references like this:

Javascript on a page loaded in one of the frames--calls a function on the main page:

window.parent.somefunc();

Javascript on a page loaded in one of the frames--calls a function on a page loaded in one of the other frames:

window.parent.otherFrameName.anotherfunc();
Copy linkTweet thisAlerts:
@inadazeauthorJan 20.2005 — It doesn't work though.

Here is my main page that opens the popup:

<html>

<head>

<title>&nbsp;</title>

<SCRIPT language=JavaScript>

function something(){

javascript:window.close();

}

function popup() {

var k = 0;

var scrollable = "no";

for(i=0;i <4;i++){

for(j=0;j<6;j++){

var a=Math.round(Math.random()*24);

var b=Math.round(Math.random()*
24);

var c=Math.round(Math.random()*24);

var d=Math.round(Math.random()*
24);

if(k==a || k==b || k==c || k==d){

scrollable = "yes";

}

window.open("Popup1/pop"+k+".htm","Pop"+k,'width=200,height=200,left='+j*200+',top='+i*220+',resizable=no,scrollbars='+scrollable+'');

scrollable = "no";

k++;

}

}

//window.open("Hidden.htm","Hidden",'width=10,height=10,left=0,top=0');

//javascript:window.close();

}

</SCRIPT>

</head>

<Body onLoad = "popup()"></Body>

</html>

And here is my pop up:

<html>

<head>

<title>popup</title>

<style type="text/css">

<!--

body {

background-color: #000000;

}

body,td,th {

color: #000099;

}

-->

</style>

<script language=JavaScript>

function writeText(){

//document.write("There once was a girl");

window.opener.something();

}

</script>

</head>

<body>

<img src = "images/popImg_01.gif" width="201" height="201" onLoad = "writeText()">

</body>

</html>

Does that look right?

Jay
Copy linkTweet thisAlerts:
@7studJan 21.2005 — Two threads going for the same question? Goodbye.
Copy linkTweet thisAlerts:
@inadazeauthorJan 22.2005 — I am sorry if I was not clear in asking my two different questions. My first thread, I wanted to know if it was possible to control windows from another window. My second question was if I could create a function in one window that could be accessed from another window. I see how this could be confusing and how the two questions are related, but this is only evident to those who, like yourself, know the answer, which I did not. Now I know.

Jay
×

Success!

Help @inadaze spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,
)...