By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,461 Members | 1,002 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,461 IT Pros & Developers. It's quick & easy.

JS in frame1 <--> JS in frame2

P: n/a
I have a page with two frames. Frame1 contains an inline script. Frame2
contains inline and 2 loaded scripts. How may I call a function in
frame1(inline) from frame2(inline or loaded)?

Albert

Jul 20 '05 #1
Share this Question
Share on Google+
17 Replies


P: n/a
Albert Wagner <al******@tcac.net> writes:
I have a page with two frames. Frame1 contains an inline script. Frame2
contains inline and 2 loaded scripts. How may I call a function in
frame1(inline) from frame2(inline or loaded)?


You have one page, which is a frameset. It contains two other pages
in frames.

To access a global variable of frame1 from frame2, you just write (in
frame2):

parent.frames['frame1'].globalVarName

<URL:file://localhost/D:/Home/lrn/html/faq/JSwindows.html#ref_3_12>

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:oe**********@hotpop.com...
<snip>
<URL:file://localhost/D:/Home/lrn/html/faq/JSwindows.html#ref_3_12>


Did you mean:-

<URL: http://www.infimum.dk/HTML/JSwindows.html#ref_3_12 >

Richard.
Jul 20 '05 #3

P: n/a
"Richard Cornford" <ri*****@litotes.demon.co.uk> writes:
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:oe**********@hotpop.com...
<URL:file://localhost/D:/Home/lrn/html/faq/JSwindows.html#ref_3_12>


Did you mean:-
<URL: http://www.infimum.dk/HTML/JSwindows.html#ref_3_12 >


DOH!
Thanks.

/L 'Must. Be. More. Careful!'
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4

P: n/a
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:k7**********@hotpop.com...
<snip>
Did you mean:-
<URL: http://www.infimum.dk/HTML/JSwindows.html#ref_3_12 >


DOH!
Thanks.


Incidentally, I liked the page, though you could have mentioned browsers
that lack the window.open function (PDA and embedded) in the "Problems
with opening windows" section.

Richard.
Jul 20 '05 #5

P: n/a
On 12 Sep 2003 00:31:25 +0200
Lasse Reichstein Nielsen <lr*@hotpop.com> wrote:
<snip>
You have one page, which is a frameset. It contains two other pages
in frames.

To access a global variable of frame1 from frame2, you just write (in
frame2):

parent.frames['frame1'].globalVarName

<URL: file://localhost/D:/Home/lrn/html/faq/JSwindows.html#ref_3_12 >


Thank you, Lasse, for both your exellent reply and the link. I've been
saveing all the good links from this NG.
Jul 20 '05 #6

P: n/a
On Thu, 11 Sep 2003 23:02:51 +0000 (UTC), "Richard Cornford"
<ri*****@litotes.demon.co.uk> wrote:
Incidentally, I liked the page, though you could have mentioned browsers
that lack the window.open function (PDA and embedded) in the "Problems
with opening windows" section.


One of the interesting things in opera 6.0 on Series 60 Symbian
devices is that javascript defaults to OFF, but pop-ups default to on,
which seems kind of odd.

(I hope to be looking further into Opera/Netfront and others on Series
60 phones so, both claim DOM/DHTML)

Jim.
--
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #7

P: n/a
"Richard Cornford" <ri*****@litotes.demon.co.uk> writes:
Incidentally, I liked the page,
Thanks!
though you could have mentioned browsers
that lack the window.open function (PDA and embedded) in the "Problems
with opening windows" section.


Consider it done and uploaded.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #8

P: n/a
On 12 Sep 2003 00:31:25 +0200
Lasse Reichstein Nielsen <lr*@hotpop.com> wrote:
<snip>

I tried your suggestion and the link, but no joy. I have included the
code and the error. Any criticisms or advice are welcome. Thanks
ahead.

Albert

========== test.html =========================================
<HTML>
<HEAD>
<TITLE>Test and Debug</TITLE>
</HEAD>
<FRAMESET ROWS="30%,70%" COLS="30%,70%">

<FRAME SRC="debugger.html" NAME="debugFrame" ID="debugFrame">
<FRAME SRC="program.html" NAME="programFrame" ID="programFrame">

</FRAMESET>
</HTML>

=========== debugger.html ====================================
<HTML>
<HEAD>
<TITLE>Debugger</TITLE>
<SCRIPT type="text/JavaScript">

var DebugPrint = function(_line) {
debugFrame.document.writeln("<p>"+_line+"</p>");
}

function Initialize() {
debugFrame = window.parent.frames.debugFrame;
DebugPrint("Debugger started.");
}

</SCRIPT>
</HEAD>
<BODY onLoad="Initialize()">
</BODY>
</HTML>

=========== program.html ========================================
<html>
<head>
<title>JavaScript-Test</title>
<script type="text/javascript">
var Print = function(_line) {
programFrame.document.writeln('<br>'+_line+'</br>');
}

function Initialize() {
programFrame = window.parent.frames.programFrame;
Print("Program started.");
window.parent.frames.debugFrame.DebugPrint("From Program"); // error
this line
}
</script>
</head>
<body onLoad="Initialize()">
</body>
</html>

============ JavaScript Console / Opera 7.11 =======================

Event thread: onload
Error:
name: TypeError
message: Statement on line 9: Expression did not evaluate to a function
object: window.parent.frames.debugFrame.DebugPrint
Backtrace:
Line 9 of inline#1 script in
file://localhost/usr/js/work/mover/program.html
window.parent.frames.debugFrame.DebugPrint("From Program");
In unknown script
Initialize();
At unknown location
{event handler trampoline}
Jul 20 '05 #9

P: n/a
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote
<URL: http://www.infimum.dk/HTML/JSwindows.html#ref_3_12 >


DOH!
Thanks.


It says:

"Referencing new windows
The "window.open" function returns a reference to the new window, or to the
old window, if it was already open. It is important to keep this reference
in order to communicate with the new window, or to close it again.
var myWindow = document.open("foo.html","windowName","resizable=n o"); "

Shouldn't be "window.open"?

And you have a typo here:
"Since "window" is a reference to the global object, the function can also
be referred to as just the global variabel "open"."

BTW, nice page. Thanks.

Vjekoslav
Jul 20 '05 #10

P: n/a
Albert Wagner <al******@tcac.net> writes:
<FRAME SRC="debugger.html" NAME="debugFrame" ID="debugFrame">
<FRAME SRC="program.html" NAME="programFrame" ID="programFrame">
I recommend against having the same "name" and "id" attribute values.

In both HTML and XHTML, "name" is a valid attribute name for frames.
If you need the "id" attribute, add "Id" to the end of it, or something
similar.

=========== debugger.html ==================================== var DebugPrint = function(_line) {
debugFrame.document.writeln("<p>"+_line+"</p>");
If we are in the debugFrame, then it suffices to write
document.writeln(...);
The scope rules will garantee that we always refer to the current
frame's document object

You should escape the "</" token. The recommended way is "<\/".

I.e.:
document.writeln("<p>"+_line+"<\/p>");
}

function Initialize() {
debugFrame = window.parent.frames.debugFrame;
This is in the debugFrame, so "debugFrame = window" should be
equivalent.
DebugPrint("Debugger started.");
Here you make a document.write to the current document. That completely
erases the current document and starts writing a new one. Erasing the
current document also loses the debugFrame function. So, if this page
loads faster than the program page, then it erases the function before
it can be called.
=========== program.html ======================================== var Print = function(_line) {
programFrame.document.writeln('<br>'+_line+'</br>');
As above. Change "</br>" to "<\/br>". The "programFrame." isn't
necessary ...
}

function Initialize() {
programFrame = window.parent.frames.programFrame;
.... and this can be shorter (or omitted)
Print("Program started.");
Here you erase the current page. The running function isn't lost before
it finishes, so the next line is executed.
window.parent.frames.debugFrame.DebugPrint("From Program"); // error
this line
If this frame loads faster than the debug frame, then the function
isn't available yet. If it is slower, the debug frame erases it again.

There is a very narrow margin where this could possibly work.
message: Statement on line 9: Expression did not evaluate to a function
object: window.parent.frames.debugFrame.DebugPrint


.... and therefore it most likely fails every time.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #11

P: n/a
"Vjekoslav Begovic" <vj*******@inet.hr> writes:
Shouldn't be "window.open"?
It should indeed. Thanks.
And you have a typo here:
"Since "window" is a reference to the global object, the function can also
be referred to as just the global variabel "open"."
I think it is correct. The sentence could be better though, so I have
done a rewrite.
BTW, nice page.


Thanks. Any other typos you should find, are welcome too

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #12

P: n/a
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:r8**********@hotpop.com...
"Vjekoslav Begovic" <vj*******@inet.hr> writes:

And you have a typo here:
"Since "window" is a reference to the global object, the function can also be referred to as just the global variabel "open"."


I think it is correct.


My english could be much better, but I think "variabel" should be
"variable".

Regards

Vjekoslav
Jul 20 '05 #13

P: n/a
"Vjekoslav Begovic" <vj*******@inet.hr> writes:
My english could be much better, but I think "variabel" should be
"variable".


So could mine, and you are correct. The eyes are the first place you
go blind :)

/L 'I *so* need to install that spelling checker again'
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #14

P: n/a
On 12 Sep 2003 10:52:45 +0200
Lasse Reichstein Nielsen <lr*@hotpop.com> wrote:
<snip>

Hi, Lasse. Thanks for another very helpful post. As you might have
guessed, I am not only a newbie to JavaScript but also to HTML. Based
on your remarks I have rewritten my code and it now appears to work. I
have included it below. If you have the time, I would appreciate
another critique. Thanks again.

Albert

=============== test.html ===================
<HTML>
<HEAD>
<TITLE>Test and Debug</TITLE>
</HEAD>
<FRAMESET ROWS="30%,70%" COLS="30%,70%">

<FRAME SRC="debugger.html" NAME="debugFrame" ID="debugFrameId">
<FRAME SRC="program.html" NAME="programFrame" ID="programFrameId">

</FRAMESET>
</HTML>

=============== debugger.html ====================
<HTML>
<HEAD>
<TITLE>Debugger</TITLE>
<SCRIPT type="text/JavaScript">

var listContents = "";

function PrintLine(_line) {
listContents += _line + "\n";
RefreshList();
}

function RefreshList() {
document.theForm.debugList.focus();
document.theForm.debugList.value = listContents;
document.theForm.debugList.blur();
}

function Initialize() {
PrintLine("Debugger loaded...");
}

</SCRIPT>
</HEAD>
<BODY onLoad="Initialize()">
<FORM name="theForm">
<TEXTAREA name="debugList" cols="40"
rows="500" readonly
onChange = "RefreshList">
</TEXTAREA>
</FORM>
</BODY>
</HTML>

============== program.html =============================
<html>
<head>
<title>Program</title>
<script type="text/javascript">

var debug;

function Initialize() {
debug = parent.frames.debugFrame.PrintLine;
debug("Program loaded...");
RunProgram();
}

function RunProgram() {
if (debug) debug("RunProgram function entered");
if (debug) debug("debug message 1");
if (debug) debug("debug message 2");
}

</script>
</head>
<body onLoad="Initialize()">
</body>
</html>
Jul 20 '05 #15

P: n/a
Albert Wagner <al******@tcac.net> writes:
it now appears to work.
Great!
I have included it below. If you have the time, I would appreciate
another critique.
Hah! The chance to indoctrinate yet another beginning web author.
How could I refuse?!?
=============== test.html ===================
Document type! Always include a document type to put the browser into
standards mode, and to allow verification with an HTML validator.

Since this is a frameset, and it isn't XHTML, I would use HTML 4.01
Frameset:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
"http://www.w3.org/TR/REC-html40/frameset.dtd">

I won't begin on the problems with frames. :)
<HTML>
<HEAD>
<TITLE>Test and Debug</TITLE>
</HEAD>
<FRAMESET ROWS="30%,70%" COLS="30%,70%">
You don't need both ROWS and COLS, do you? This creates a frameset
with four frames, and fills it from the top left:

+------+--------------+
|debug |program |
| | |
| | |
+------+--------------+
| | |
| | |
| | |
| | |
| | |
| | |
| | |
+------+--------------+
<FRAME SRC="debugger.html" NAME="debugFrame" ID="debugFrameId">
<FRAME SRC="program.html" NAME="programFrame" ID="programFrameId">
You need another two frames to fill the frameset (or just skip ROWS or
COLS).

</FRAMESET>
</HTML>

=============== debugger.html ====================
Document type! This looks like HTML 4.01, possibly even strict.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/REC-html40/strict.dtd">
<HTML>
<HEAD>
<TITLE>Debugger</TITLE>
<SCRIPT type="text/JavaScript">

var listContents = "";

function PrintLine(_line) {
Why the underscore?
<BODY onLoad="Initialize()">
Good choice making an initialization function, instead of putting
too much code in the onload attribute.
function RunProgram() {
if (debug) debug("RunProgram function entered");
if (debug) debug("debug message 1");
if (debug) debug("debug message 2");


One "if (debug)" should be sufficient :)

If you add the document types and validate the code, you might need to
add som paragraphs or divs around the input/textarea elements.

That's about it. It looks pretty good now :)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #16

P: n/a
On 12 Sep 2003 23:05:59 +0200
Lasse Reichstein Nielsen <lr*@hotpop.com> wrote:

<snip>
I won't begin on the problems with frames. :)
Yes. I've read that they have been deprecated, but haven't yet read
enough to understand how to achive the same thing in XHTML.

<snip> You don't need both ROWS and COLS, do you? This creates a frameset
with four frames, and fills it from the top left:
No. You are right. Just COLS is all I need.

<snip> Why the underscore?
I have gotten into the habit of using a leading underscore for all
function arguments:

function x( _a, _b, _c) {
this.b = _b;
var c = _c + 1;
d;

function inner(){
doSomethingWith(_a);
doSomethingWith(_b);
}
}

Is this not a good habit? I assume that the scope of the arguments
extends into internal functions just like a function's declared
variables. This allows me to use them downstream without having to
declare vars for them or unneccessarily uglify with this. prefixes.

<snip>
function RunProgram() {
if (debug) debug("RunProgram function entered");
if (debug) debug("debug message 1");
if (debug) debug("debug message 2");


One "if (debug)" should be sufficient :)


Right.

<snip> That's about it. It looks pretty good now :)


Thank you. But actually I discovered a problem with the textarea
content using the onChange, focus, and blur methods. Scrolling also
triggered onChange for the textarea, and focus/blur were totally
unnecessary. So I removed all of that from the code below. Apparently,
at least in Opera 7.11, all that is required to refresh the textarea is
to simply set the .value again.

Thanks for your help. I now have a nifty little harness for
tracing/debugging my JavaScript code.

================= debugger.html =============================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/REC-html40/strict.dtd">
<HTML>
<HEAD>
<TITLE>Debugger</TITLE>
<SCRIPT type="text/JavaScript">

var listContents = "";
var lineCnt = 0;

function PrintLine(_line) {
listContents += lineCnt+" "+ _line + "\n";
lineCnt++;
document.theForm.debugList.value = listContents;
}

function Initialize() {
PrintLine("Debugger loaded...");
}

</SCRIPT>
</HEAD>
<BODY onLoad="Initialize()">
<FORM name="theForm">
<TEXTAREA name="debugList" cols="40"
rows="500" readonly>
</TEXTAREA>
</FORM>
</BODY>
</HTML>
--
Don't you see that the whole aim of Newspeak is to narrow the range of
thought? In the end we shall make thoughtcrime literally impossible,
because there will be no words in which to express it.
-- George Orwell, 1984

Jul 20 '05 #17

P: n/a
Albert Wagner <al******@tcac.net> writes:
I have gotten into the habit of using a leading underscore for all
function arguments: .... Is this not a good habit?
See below.
I assume that the scope of the arguments extends into internal
functions just like a function's declared variables.
This allows me to use them downstream without having to declare vars
for them or unneccessarily uglify with this. prefixes.


Correct (and this.-prefixes wouldn't work anyway).

As you say, the function arguments work exactly like declared local
variables. And I do mean *exactly*, they are all properties of the
so-called "variable object" that is created for each function
invocation.

Since there is nothing distinguishing arguments and local variables, I
can see no reason for prefixing one of them with an underscore.

If you are afraid to shadow one of the local variables with an argument
of an inner function, then the prefixing will stop that problem. It won't
prevent you from shadowing the argument of the outer function, so you
still have to worry about the names.

I can't say if it is a good or bad habit. I don't like it, because it
draws attention to some of the variable names, for no reason. But it
is definitly a matter of taste, not anything else.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #18

This discussion thread is closed

Replies have been disabled for this discussion.