尝试从另一个帧访问一帧中的JavaScript变量。 [英] Trying to access a JavaScript variable in one frame from another frame.

查看:60
本文介绍了尝试从另一个帧访问一帧中的JavaScript变量。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,我知道,不要使用框架。好吧,我坚持使用这些框架和

我试图在没有完整redsign的情况下添加功能。你可以把这看作是一个怀旧的旅程。


无论如何,我在顶层有以下框架结构:


FRAMESET代码

<!DOCTYPE HTML PUBLIC" - // W3C // DTD HTML 4.01 Frameset // EN">

< html>< head>< title>服务器列表< / title>< / head>

< frameset frameborder =" 1"边界=" 1 QUOT; framespacing = QUOT 1 QUOT; rows =" 10%,89%">

< frame name =" TopFrame"

src =" /rob/serverlist.nsf/TopPage ?OpenPage"

SCROLLING =" No" NORESIZE FRAMEBORDER =" No">

< frameset cols =" 16%,83%">

< frame name =" LeftNavFrame"

src =" /rob/serverlist.nsf/LeftNavPage?OpenPage& amp; BaseTarget = MainFrame"

SCROLLING =" No" NORESIZE FRAMEBORDER =" No">

< frameset rows =" 8%,91%">

< frame name =" ViewControls"

src =" /rob/serverlist.nsf/by%20Server%20Name?OpenPage& amp; BaseTarget = MainFra me"

SCROLLING =" No" NORESIZE FRAMEBORDER =" No">

< frame frameborder =" 0" name =" MainFrame"

src =" /rob/serverlist.nsf/by%20Server%20Name?OpenView">

< / frameset>

< / frameset>

< / frameset>< / html>


我正试图提升价值我在另一帧中设置了一个

帧的JavaScript变量。这是详细信息。


在框架''MainFrame''我有这个代码(它使用prototype.js来

获取字段值但是代码的那部分可以工作。在框架中找到代码似乎不会是b
。):

主框架代码碎片


.... snip

< body text ="#000000" BGCOLOR = QUOT;#E0FFFF" id =''viewbody''>

< form action ="">

< script> var resort =''RESORT VALUE HERE'' ;< / script>

.... snip


END MAINFRAME CODE FRAGMENT


in frame' 'ViewControls''我有这个代码:

VIEWCONTROLS代码片段


.... snip

< script> < br $>
函数getFrame(fName){

var frames = top.document.getElementsByTagName(" FRAME");

var frame = null; < ($ var $ 0; i< frames.length; i ++){

if(frames [i] .name == fName)

frame = frames [i];

}

var forms = frame.getElementsByTagName(" *");

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]);

//}

返回null;

}


var viewStart = 1;

函数openView(增量){

viewStart = parseInt(viewStart)+ parseInt(increment);

viewStart =(viewStart< 1)? 1:viewStart;

alert(" Frame found:" +

getFrame(" MainFrame")。document.forms [0] .resort);

window.open($ F(''URLtoOpen'')+"& Start =" + viewStart +"& Count ="

+

$ F(计数),''MainFrame'');

}


< / script>

要显示的行:

< input name =" Count"值= QUOT; 1000" ID = QUOT;计数"大小= QUOT; 4英寸maxlength =" 4">

< input type =" button"的onclick = QUOT; viewStart = 1;的OpenView(0);" value =" First

Page">

< input type =" button" onclick =" openView( - ($ F(''Count'')));"

value ="<< Previous Page">

< input type =" button"的onclick = QUOT;的OpenView($ F(计数));" value ="下一页

Page>>">

< script> openView(0);< / script>

.... snip


END VIEWCONTROLS代码片段

因此当ViewControls页面加载时调用openView函数并且

当按下三个按钮中的一个时。现在openView在警报中调用了

getFrame(" MainFrame"),因为我正在测试。 getFrame

successfuly找到" MainFrame"但是我被卡住了。


DOM阅读并使用它(打开和

关闭)几个月后仍然让我感到困惑。我无法弄清楚

元素的层次结构。当我在FireFox中使用DOM检查器时,它向我显示了FRAME中名称=" MainFrame"的
。是#document (#

是什么意思?)。我可以通过孩子们追踪文档 - > HTML-> BODY-> FORM''。

这不是创建变量的形式吗?然而,我找不到

它使用DOM检查器,我已经尝试了我能想到的所有内容

找到它的代码。


显然,我对DOM中的事物范围感到困惑。任何人都可以将b $ b指向我的书籍或网站,清楚地解释了什么地方和

为什么? (或任何其他帮助。)


提前致谢,


Rob: - ]

解决方案

F(''URLtoOpen'')+"& Start =" + viewStart +"& Count ="

+


F(" Count"),''MainFrame'');

}


< / script>

要显示的行:

<输入名称= QUOT;计数"值= QUOT; 1000" ID = QUOT;计数"大小= QUOT; 4英寸maxlength =" 4">

< input type =" button"的onclick = QUOT; viewStart = 1;的OpenView(0);" value =" First

Page">

< input type =" button" onclick =" openView( - (


F(''Count'')));"

value ="<<< Previous Page" ;>

< input type =" button"的onclick = QUOT;的OpenView(

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><title>Server List</title></head>
<frameset frameborder="1" border="1" framespacing="1" rows="10%,89%">
<frame name="TopFrame"
src="/rob/serverlist.nsf/TopPage?OpenPage"
SCROLLING="No" NORESIZE FRAMEBORDER="No">
<frameset cols="16%,83%">
<frame name="LeftNavFrame"
src="/rob/serverlist.nsf/LeftNavPage?OpenPage&amp;BaseTarget=MainFrame"
SCROLLING="No" NORESIZE FRAMEBORDER="No">
<frameset rows="8%,91%">
<frame name="ViewControls"
src="/rob/serverlist.nsf/by%20Server%20Name?OpenPage&amp;BaseTarget=MainFra me"
SCROLLING="No" NORESIZE FRAMEBORDER="No">
<frame frameborder="0" name="MainFrame"
src="/rob/serverlist.nsf/by%20Server%20Name?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="#E0FFFF" 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.getElementsByTagName("FRAME");
var frame= null;
for(var i=0; i < frames.length; i++){
if(frames[i].name == fName)
frame = frames[i];
}
var forms = frame.getElementsByTagName("*");
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(increment){
viewStart = parseInt(viewStart) + parseInt(increment);
viewStart = (viewStart < 1) ? 1 : viewStart;
alert("Frame found: " +
getFrame("MainFrame").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="viewStart=1;openView(0);" value="First
Page">
<input type="button" onclick="openView(-($F(''Count'')));"
value="<<Previous Page">
<input type="button" onclick="openView($F(''Count''));" value="Next
Page>>">
<script>openView(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("MainFrame") 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:-]

解决方案

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="viewStart=1;openView(0);" value="First
Page">
<input type="button" onclick="openView(-(


F(''Count'')));"
value="<<Previous Page">
<input type="button" onclick="openView(


这篇关于尝试从另一个帧访问一帧中的JavaScript变量。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆