|
-
Sep 20th, 2003, 06:57 PM
#1
Thread Starter
Hyperactive Member
JavaScript (DOM): Code across frames?
Hi,
What I need is to access document objects in one frame from another, to call functions in such frame, to share one variable in all frames of document and so on. Somewhere (perhaps on MSDN ) I read this is disabled because of security, but I would like to allow my code, objects, functions, variables, ... to be 'inherited' by child (or same leveled) iframes.
Is there any way how to do it? Or how is this problem being solved?
Thanks,
John
-
Sep 23rd, 2003, 01:59 AM
#2
I never heard of that being disabled, and code like this has always worked for me:
Code:
<frame name="head" id="head" src="bla.html" />
<frame name="body" id="body" src="flop.html" />
// flop.html:
<script type="text/javascript">
function xyz()
{
alert(window.parent.frames['head'].document.getElementById('searchbox').value);
}
</script>
// bla.html
<script type="text/javascript">
function onButtonClick()
{
window.parent.frames['body'].xyz();
}
</script>
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 24th, 2003, 05:45 AM
#3
Thread Starter
Hyperactive Member
Well the problem I think is will work only in one level of frames. In the next one, you would use window.parent.parent.document to acces the main document, then parent.parent.parent and so on.
Although I found this method quite strange, the problem could be solved ... until ... until I don't know what the calling frame is level of.
Even this can be done, using window.parent.parent and many 'preventive' .parents added to it.
Actually, my situation is (simplified) like this:
Code:
Root frame
|
+-- Child1A
| |
| '---- Child2A
|
+-- Child1B
|
'---- Child2B
|
'---- Child3
and the task is to acces Child2A elements from Child3, without knowledge that Child3 is actually '3rd child' and Child2A being 'child of 1A'.
So looking for something like wholebrowserwindow.allnestedframes('Child2A') or at least to simply get true parent, resp. browser window.
-
Sep 24th, 2003, 10:35 AM
#4
When do you have a frameset page in a frameset page?
For nested frameset tags in one page there's only one parent object.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 24th, 2003, 12:26 PM
#5
Thread Starter
Hyperactive Member
Well actually I don't have any frameset page , I have standard page, with about three iframes in it, in which there are some other nested iframes and so on...
-
Sep 24th, 2003, 12:35 PM
#6
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 24th, 2003, 12:40 PM
#7
But actually I see no reason why it shouldn't work for more than one level!
Code:
base.html
<iframe id="ifr1" src="ifr1.html">No iframes</iframe>
<iframe id="ifr2" src="ifr2.html">No iframes</iframe>
Code:
ifr1.html
<iframe id="ifr11" src="ifr11.html">No iframes</iframe>
Code:
ifr2.html
<script type="text/javascript">
function hideSomething()
{
window.parent.document.getElementById(
'ifr1').document.getElementById('ifr11').
document.getElementById('hideable').
style.visibility = "hidden";
}
</script>
Code:
ifr11.html
<script type="text/javascript">
function callWeird()
{
window.parent.parent.document.
getElementById('ifr2').hideSomething();
}
</script>
To lazy to try it, but I think it should work.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 24th, 2003, 12:54 PM
#8
But of course that would require you to know the exact structure...
Hmmm...
Code:
function getAllIFrames()
{
var ret = new Array();
var par = window;
while(par.parent) par = par.parent;
var allframes = par.document.getElementsByTagName('iframe');
for(var i = 0; i < allframes.length; ++i) {
ret[allframes[i].id] = allframes[i];
var locals = allframes[i].document.getElementsByTagName('iframe');
for(var j = 0; j < locals.length; ++j) {
allframes[allframes.length] = locals[j];
}
}
return ret;
}
Make sure all the iframes have id attributes and they are ALL unique all across the pages.
Now if that works I really have to pat myself on the back
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 24th, 2003, 02:08 PM
#9
Thread Starter
Hyperactive Member
Wow, cool ! I was thinking about enumerating all children iframes from parent document, but doing this trick to find parent within iframe... you've got me
Thank you for help, I belive this will work.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|