473,699 Members | 2,416 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trying to access a JavaScript variable in one frame from another frame.

Rob
I know, I know, don't use frames. Well, I'm stuck with these frames and
I'm trying to add functionality without a complete redsign. You can
look at this as a nostalgic journey.

Anyway, I've got the following frame structure at the top level:

FRAMESET CODE
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html><head><ti tle>Server List</title></head>
<frameset frameborder="1" border="1" framespacing="1 " rows="10%,89%">
<frame name="TopFrame"
src="/rob/serverlist.nsf/TopPage?OpenPag e"
SCROLLING="No" NORESIZE FRAMEBORDER="No ">
<frameset cols="16%,83%">
<frame name="LeftNavFr ame"
src="/rob/serverlist.nsf/LeftNavPage?Ope nPage&amp;BaseT arget=MainFrame "
SCROLLING="No" NORESIZE FRAMEBORDER="No ">
<frameset rows="8%,91%">
<frame name="ViewContr ols"
src="/rob/serverlist.nsf/by%20Server%20N ame?OpenPage&am p;BaseTarget=Ma inFrame"
SCROLLING="No" NORESIZE FRAMEBORDER="No ">
<frame frameborder="0" name="MainFrame "
src="/rob/serverlist.nsf/by%20Server%20N ame?OpenView">
</frameset>
</frameset>
</frameset></html>

I'm trying to pickup the value of a JavaScript variable I set in one
frame from another frame. Here's the details.

In frame 'MainFrame' I have this code (it makes use of prototype.js to
fetch field values but that part of the code works. It just can't seems
to find code across frames.):
MAINFRAME CODE FRAGMENT

....snip
<body text="#000000" bgcolor="#E0FFF F" id='viewbody'>
<form action="">
<script>var resort = 'RESORT VALUE HERE';</script>
....snip

END MAINFRAME CODE FRAGMENT

In frame 'ViewControls' I have this code:
VIEWCONTROLS CODE FRAGMENT

....snip
<script>
function getFrame(fName) {
var frames = top.document.ge tElementsByTagN ame("FRAME");
var frame= null;
for(var i=0; i < frames.length; i++){
if(frames[i].name == fName)
frame = frames[i];
}
var forms = frame.getElemen tsByTagName("*" );
for(var j=0; j < forms.length; j++){
alert("Got form " + j);
}
// for(var n in frame.document. forms[0]){
// alert("Frame val: " + n + " is " +
frame.document. forms[0][n]);
// }
return null;
}

var viewStart = 1;
function openView(increm ent){
viewStart = parseInt(viewSt art) + parseInt(increm ent);
viewStart = (viewStart < 1) ? 1 : viewStart;
alert("Frame found: " +
getFrame("MainF rame").document .forms[0].resort);
window.open($F( 'URLtoOpen') + "&Start=" + viewStart + "&Count="
+
$F("Count"), 'MainFrame');
}

</script>
Rows to display:
<input name="Count" value="1000" id="Count" size="4" maxlength="4">
<input type="button" onclick="viewSt art=1;openView( 0);" value="First
Page">
<input type="button" onclick="openVi ew(-($F('Count'))); "
value="<<Previo us Page">
<input type="button" onclick="openVi ew($F('Count')) ;" value="Next
Page>>">
<script>openVie w(0);</script>
....snip

END VIEWCONTROLS CODE FRAGMENT
So the function openView is called when the ViewControls page loads and
when one of the three buttons is pressed. Right now openView calls
getFrame("MainF rame") in the alert because I'm testing. getFrame
successfuly finds "MainFrame" but there I get stuck.

The DOM still confounds me after reading and working with it (on and
off) for months. I can not figure out what the hierarchy is of the
elements. When I use the DOM inspector in FireFox it shows me that
inside the FRAME with name="MainFrame " is "# document" (what does the #
mean?). I can trace through the children "document->HTML->BODY->FORM'.
Isn't this the form where the variable was created? Yet I can not find
it using the DOM inspector and I've tried everything I can think of in
the code to find it.

Clearly I am confused about the scope of things in the DOM. Can anyone
point me to a book or web site that CLEARLY explains what is where and
why? (Or any other help.)

Thanks in advance,

Rob:-]

May 16 '06 #1
11 16950
ASM
Rob a écrit :
I know, I know, don't use frames. Well, I'm stuck with these frames and
I'm trying to add functionality without a complete redsign. You can
look at this as a nostalgic journey.

Anyway, I've got the following frame structure at the top level:

FRAMESET CODE
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html><head><ti tle>Server List</title></head>
<frameset frameborder="1" border="1" framespacing="1 " rows="10%,89%">
<frame name="TopFrame"
src="/rob/serverlist.nsf/TopPage?OpenPag e"
SCROLLING="No" NORESIZE FRAMEBORDER="No ">
<frameset cols="16%,83%">
<frame name="LeftNavFr ame"
src="/rob/serverlist.nsf/LeftNavPage?Ope nPage&amp;BaseT arget=MainFrame "
SCROLLING="No" NORESIZE FRAMEBORDER="No ">
<frameset rows="8%,91%">
<frame name="ViewContr ols"
src="/rob/serverlist.nsf/by%20Server%20N ame?OpenPage&am p;BaseTarget=Ma inFrame"
SCROLLING="No" NORESIZE FRAMEBORDER="No ">
<frame frameborder="0" name="MainFrame "
src="/rob/serverlist.nsf/by%20Server%20N ame?OpenView">
</frameset>
</frameset>
</frameset></html>

I'm trying to pickup the value of a JavaScript variable I set in one
frame from another frame. Here's the details.

In frame 'MainFrame' I have this code (it makes use of prototype.js to
fetch field values but that part of the code works. It just can't seems
to find code across frames.):
MAINFRAME CODE FRAGMENT

...snip
<body text="#000000" bgcolor="#E0FFF F" id='viewbody'>
<form action="">
<script>var resort = 'RESORT VALUE HERE';</script>
...snip

END MAINFRAME CODE FRAGMENT

In frame 'ViewControls' I have this code:
VIEWCONTROLS CODE FRAGMENT

...snip
<script>
function getFrame(fName) {
var frames = top.document.ge tElementsByTagN ame("FRAME");
var frames = top.frames;
var frame= null;
for(var i=0; i < frames.length; i++){
if(frames[i].name == fName)
frame = frames[i];
}
var forms = frame.getElemen tsByTagName("*" );
var forms = frame.document. forms;
or
var forms = frame.document. getElementsByTa gName("*");
but will not be your forms (all tags)
so :
var forms = frame.document. getElementsByTa gName("FORM");
^^^^^^^^ ^^^^
for(var j=0; j < forms.length; j++){
alert("Got form " + j);
alert('got tag '+j+' - tagname = '+forms[j].tagName);

}
// for(var n in frame.document. forms[0]){
// alert("Frame val: " + n + " is " +
frame.document. forms[0][n]);
non sense : you don't know if you are in a form
(could be body or div or...)
// }
return null;
}


[snip]

--
Stephane Moriaux et son [moins] vieux Mac
May 16 '06 #2
Rob
I've messed up my code trying to get something to work. I tried your
code like this:

function getFrame(fName) {
var frames = top.document.ge tElementsByTagN ame("FRAME");
var frame= null;
for(var i=0; i < frames.length; i++){
if(frames[i].name == fName)
frame = frames[i];
}
var forms = frame.document. forms;
alert("Got this many forms: " + forms.length); // Should be one
un-named form
return null;
}

On this line: var forms = frame.document. forms
I get the error message, "frame.docu ment has no properties".
That is my problem in a nutshell. The DOM inspector shows that frame
has one property it labels as "# document". (What does the # mean?)

That's where I'm stuck.

Thanks for helping.

Rob:-]

May 16 '06 #3
Rob wrote:
I've messed up my code trying to get something to work. I
tried your code like this:

function getFrame(fName) {
var frames = top.document.ge tElementsByTagN ame("FRAME");
var frame= null;
for(var i=0; i < frames.length; i++){
if(frames[i].name == fName)
frame = frames[i];
}
var forms = frame.document. forms;
alert("Got this many forms: " + forms.length); // Should be one
un-named form
You want to watch letting your posting software wrap your code, as that
tends to render it syntactically incorrect (so we cannot tell whether
the incorrectness is in the original or an artefact of poor posting
style). Generally, arranging that posted code has no lines longer than
(about) 72 characters (and is indented with (2 to 4) spaces rather than
tabs) will avoid problems.
return null;
}

<snip>

Window/global objects (in browsers that support frames) have a -
frames - collection, in which the global/window object of all contained
frames can be referenced by NAME attribute or integer index. So
referencing the window/global object of a frame named 'LeftNavFrame' in
the top frame (the frameset page) from within any contained frame (or
the top frame itself) could be as simple as - frame =
top.frames['LeftNavFrame']; -. The resulting reference to the frame's
window/global object will be a reference to an object with a -
document - property that contains the document for that frame.

Richard.
May 16 '06 #4
Rob

Richard Cornford wrote:
Rob wrote: <snip> You want to watch letting your posting software wrap your code, as that
tends to render it syntactically incorrect (so we cannot tell whether
the incorrectness is in the original or an artefact of poor posting
style). Generally, arranging that posted code has no lines longer than
(about) 72 characters (and is indented with (2 to 4) spaces rather than
tabs) will avoid problems.
Thanks for the tip on posting. I'll try to be more careful in the
future.

<snip> Window/global objects (in browsers that support frames) have a -
frames - collection, in which the global/window object of all contained
frames can be referenced by NAME attribute or integer index. So
referencing the window/global object of a frame named 'LeftNavFrame' in
the top frame (the frameset page) from within any contained frame (or
the top frame itself) could be as simple as - frame =
top.frames['LeftNavFrame']; -. The resulting reference to the frame's
window/global object will be a reference to an object with a -
document - property that contains the document for that frame.
Thanks for the tip about finding the frame. However, as I said in my
post, finding the frame isn't the problem. The code I posted above does
find the frame. What I can't seem to find is the document in the frame
or any form in the frame. As I said abouve, that's where I'm stuck.
Richard.
So here's a quote from my original post which states more clearly where
I'm stuck and what I'd like in the way of help. I would really like to
find some documentation on the DOM that clearly tells me what you told
me and all the other things about the DOM that I don't understand
without having to beg for help on usenet. I've become skilled at many
aspects of programming but the DOM still eludes me somehow.

Rob wrote:
<snip> So the function openView is called when the ViewControls page loads and
when one of the three buttons is pressed. Right now openView calls
getFrame("MainF rame") in the alert because I'm testing. getFrame
successfuly finds "MainFrame" but there I get stuck.

The DOM still confounds me after reading and working with it (on and
off) for months. I can not figure out what the hierarchy is of the
elements. When I use the DOM inspector in FireFox it shows me that
inside the FRAME with name="MainFrame " is "# document" (what does the #
mean?). I can trace through the children "document->HTML->BODY->FORM'.
Isn't this the form where the variable was created? Yet I can not find
it using the DOM inspector and I've tried everything I can think of in
the code to find it.

Clearly I am confused about the scope of things in the DOM. Can anyone
point me to a book or web site that CLEARLY explains what is where and
why? (Or any other help.)

<snip>

Peace, Love, Laugher,

Rob:-]

May 16 '06 #5
Rob wrote:
<snip>
Thanks for the tip about finding the frame. However,
as I said in my post, finding the frame isn't the problem.
The code I posted above does find the frame.
No it does not. The - top.document.ge tElementsByTagN ame("FRAME"); - call
returns a collection of FRAME _elements_ in the top document, if you
what easy access to the documents contained in the frames you should go
through the window/global object for each frame, so through the -
frames - collection.
What I can't seem to find is the document in the
frame or any form in the frame.

<snip>

As your collection is a collection of FRAME elements you will not find
that they have - document - properties except where they do, and those -
document - properties may refer to the document that contains the
element (i.e. top.document) on browsers such as IE 5.

You will find browsers where the FRAME elements have a -
contentDocument - property that is a reference to the contained
document, but going through the - frames - collection is as consistent,
cross-browser and reliable a method as is available.

Richard.
May 17 '06 #6
Rob
Hi Richard,

Thanks for helping. I've got some questions below.

Richard Cornford wrote:
Rob wrote:
<snip>
Thanks for the tip about finding the frame. However,
as I said in my post, finding the frame isn't the problem.
The code I posted above does find the frame.
No it does not. The - top.document.ge tElementsByTagN ame("FRAME"); - call
returns a collection of FRAME _elements_ in the top document, if you
what easy access to the documents contained in the frames you should go
through the window/global object for each frame, so through the -
frames - collection.


Well I know I'm misunderstandin g something. I though frames were frame
elements. I still don't understand what you are saying. Can you write a
code snippet that shows me how to return the value from an input with
id='GetThis' on a frame named MainFrame.
What I can't seem to find is the document in the
frame or any form in the frame.

<snip>

As your collection is a collection of FRAME elements you will not find
that they have - document - properties except where they do, and those -
document - properties may refer to the document that contains the
element (i.e. top.document) on browsers such as IE 5.

You will find browsers where the FRAME elements have a -
contentDocument - property that is a reference to the contained
document, but going through the - frames - collection is as consistent,
cross-browser and reliable a method as is available.


Where is this documented so I can look it up.

Richard.


May 17 '06 #7
Rob wrote:
Hi Richard,

Thanks for helping. I've got some questions below.

Richard Cornford wrote:
Rob wrote:
<snip>
Thanks for the tip about finding the frame. However,
as I said in my post, finding the frame isn't the problem.
The code I posted above does find the frame.

No it does not. The - top.document.ge tElementsByTagN ame("FRAME"); - call
returns a collection of FRAME _elements_ in the top document, if you
what easy access to the documents contained in the frames you should go
through the window/global object for each frame, so through the -
frames - collection.


Well I know I'm misunderstandin g something. I though frames were frame
elements. I still don't understand what you are saying. Can you write a
code snippet that shows me how to return the value from an input with
id='GetThis' on a frame named MainFrame.


Frames are HTML elements and also DOM objects. The
document.getEle mentsByTagName( ) method returns a collection, which is a
DOM object but it isn't a DOM element object or an HTML element.

<URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-75708506>

e.g. (in the example I'll use divs because it's simpler, frames work the
same way for getElementsByTa gName())

<html><title>Te st getElementsByTa gName</title>
<div id="firstDiv">h ere is a div</div>
<div id="secondDiv"> here is another div</div>

<script type="text/javascript">
var divs = document.getEle mentsByTagName( 'div');
alert(
'There are ' + divs.length + ' divs'
+ '\nThe first has id ' + divs[0].id
+ '\nThe scond has id ' + divs[1].id
);
</script>
</html>

In the above, document.getEle mentsByTagName( 'div') returns a collection
of the two divs. A reference to the collection is assigned to the
variable 'divs'.

To access the first div, use divs[0] and to access the second use divs[1].

A collection is a bit like an array - it has a special length property
and the members can be accessed by index. If there is only one frame in
a document, then:

var f = document.getEle mentsByName('fr ame')

will return a collection with a length of one. To access that element,
use - f[0] - just like an array (but please remember, collections aren't
arrays).

What I can't seem to find is the document in the
frame or any form in the frame.

<snip>

As your collection is a collection of FRAME elements you will not find
that they have - document - properties except where they do, and those -
document - properties may refer to the document that contains the
element (i.e. top.document) on browsers such as IE 5.

You will find browsers where the FRAME elements have a -
contentDocument - property that is a reference to the contained
document, but going through the - frames - collection is as consistent,
cross-browser and reliable a method as is available.


Where is this documented so I can look it up.


Rather than using the document.getEle mentsByTagName( ) method, some
browsers (e.g. IE) have a document.frames collection which is similar to
other collections like document.images and document.links.

Unfortunately, the frames collection isn't part of the W3C DOM 2 HTML
specification and it isn't supported by all browsers - e.g. Firefox.

The properties of the HTMLDocument interface are here:

<URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-26809268>

There are a number of collections:

readonly attribute HTMLCollection images;
readonly attribute HTMLCollection applets;
readonly attribute HTMLCollection links;
readonly attribute HTMLCollection forms;
readonly attribute HTMLCollection anchors;
'frames' isn't among them.
--
Rob
Group FAQ: <URL:http://www.jibbering.c om/faq/>
May 17 '06 #8
RobG wrote:
[...]
A collection is a bit like an array - it has a special length property
and the members can be accessed by index. If there is only one frame in
a document, then:

var f = document.getEle mentsByName('fr ame')

will return a collection with a length of one. To access that element,
use - f[0] - just like an array (but please remember, collections aren't
arrays).


Forgot to mention that collections are also 'live'. If you add another
div, it is automatically added to the collection, you don't have to do
getElementsByTa gName('div') again to update it:
<div id="firstDiv">h ere is a div</div>
<div id="secondDiv"> here is another div</div>

<script type="text/javascript">

var divs = document.getEle mentsByTagName( 'div');

alert(divs.leng th); // Shows 2

var d = document.create Element('div');
document.body.a ppendChild(d);

alert(divs.leng th); // Shows 3

</script>
[...]
--
Rob
Group FAQ: <URL:http://www.jibbering.c om/faq/>
May 17 '06 #9
RobG wrote:
<snip>
Rather than using the document.getEle mentsByTagName( )
method, some browsers (e.g. IE) have a document.frames
collection
I was not proposing the use of the - document.frames - collection at all
(as it is not cross-browser), I was proposing the use of the global -
frames - collection. The - frames - collection (the global one or the
document one) are not related to - getElementsByTa gName - at all as they
are collections of window/global objects not a collection of elements.
which is similar to other collections like document.images
and document.links.

Unfortunately, the frames collection isn't part of the W3C
DOM 2 HTML specification and
The W3C DOM specifications do not mandate any properties of the global
object in HTML web browsers.
it isn't supported by all browsers - e.g. Firefox.


The global - frames - collection is supported by all browsers that
understand frames, including Firefox. If that were not the case it would
not be the most consistent and cross browser method of accessing the
content of frames.

Richard.
May 17 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4452
by: Steve | last post by:
Hi, I have a private website for 20 people that is similar to a web email client like hotmail. There are two frames, one on the left with links for "New", "History", "Todays" and a frame on the right with a table for viewing the contents of these messages. The left pane checks back with the server every few seconds to see if any new messages need to be processed and updates count accordingly.
5
5736
by: Nick | last post by:
OK Guys and Gals, I've got a script that does some work with the document object in another frame. This works fine until the other frame contains a document on another domain. This of course produces a permission error. I don't actually want to do anything with the document if the page is on a different domain BUT I do want to detect this situation before it
2
2427
by: A. Jamie Cuticchia | last post by:
I have a webpage with 2 frames. Each frame accesses a different URL. When I try to run a javascript from Frame 1 to access content in Frame 2, I get a forbidden error. Is there a solution to this? Thank you for your time and assistance. Jamie
9
4520
by: CW | last post by:
I wrote an HTML based chat application. The front end is built entirely on HTML + javascript. Essentially, I have a hidden frame that's refreshed frequently and any new messages are displayed in another frame using document.write. My problem is that since chat screen can be obscured by other applications/windows, I have no way of informing users that new messages have arrived other than popping the window to the top using window.focus....
1
1886
by: clemenr | last post by:
Hi. I would like to know if the following is feasible in JavaScript. I would create a frameset where the top frame includes JavaScript. The bottom frame would be directed to the login page of an existing content management system. The javascript would then fill in the login name and password of the form, and submit the form to log in. If this works then the bottom frame would be replaced with another page requesting that a link be...
5
2422
by: Dan | last post by:
We have a simple site. It's a frameset with two frames a left and a right. The left frame is essentially a list of records from a database (using a server-side repeater control). When you click on one of the items in the left frame, it targets the right frame and displays a form prefilled with information for the item you clicked. The problem is the left frame's list just shows the names of the items, and the name of the item is...
1
2287
by: mailing | last post by:
Hello, I am hoping that someone can point me in the right direction. I have 4 frames to my site. Two Rows, Two Columns. |--------------------------------|-------------------------| | | | | 1 | 2 | | | | |--------------------------------|-------------------------| | ...
2
5637
by: I Hate My Computer | last post by:
I am using frames on a website. The title link on the title page adds a query string. The link goes to a page with two rows the second has two columns. I want the right column to be changed depending on what the query string is. I have the script to parse the query string here: function getQueryVariable(variable) { var query = window.location.search.substring(1); var vars = query.split("&"); for (var i=0;i<vars.length;i++) { var pair =...
0
8694
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
8621
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9182
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9041
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
6538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4634
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3060
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
2355
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.