在不同的框架中使用变量

主框架页面
<html>
<frameset cols="150,*">
<frame src="frame1.html" name="left_side">
<frame src="frame2.html" name="right_side">
</frameset>
</html>
左面是 frame1.html页面
<html>
<head>
<script language="Javascript">
var thename="";
var thefood="";
</script>
</head>
<body>
This is frame1.html, it holds the variable values. you can put any content you like here.
</body>
</html>
设置了变量 thename和thefood,用于变量的赋值和读取
右面是 frame2.html页面
<html>
<head>
<script language="JavaScript">
function store_info()
{
  top.left_side.thename
=document.f1.yourname.value;
  self.location
="frame3.html";
}

</script>
</head>
<body>
I 'd like to get your name. please enter it below.
<p>
<form name="f1">
Your Name:
<input type="text" name="yourname" size="25">
<p>
<input type="button" value="Continue" onClick="store_info();">
</form>
</body>
</html>


里面 点击button,把文本框里面的值赋给了 变量 frame1.thename ,同时弹出self.location=frame3.html页面


frame3.html页面代码

<html>
<head>
<script language="Javascript">
function more_info()
{
  top.left_side.thefood
=document.f2.yourfood.value;
  self.location
="frame4.html";
}

</script>
</head>
<body onLoad="document.f2.yourname.value=top.left_side.thename;">
<script language="JavaScript">
document.write(
"Hi,"+top.left_side.thename+"!<br>");
</script>
Now I'd like to get your favorite food ,please enter it below;
<p>
<form name="f2">
Your Name:
<input type="text" name="yourname" size="25">
<br>
Favorite Food:
<input type="text" name="yourfood" size="25">
<p>
<input type="button" value="Continue" onClick="more_info();">
</form>
</body>
</html>

frame3.html页面读取变量top.left_side.thename(frame1.thename)的内容
同时点击button ,把text文本框的内容赋值到frame1.thefood变量里面,弹出frame4.html
<html>
<head>
<script language="javascript">
function print_info()
{
  document.write(
"Thank you,"+top.left_side.thename+"!");  
  document.write(
"<p>");
  document.write(
"you must really like "+top.left_side.thefood+"!");  
}

</script>
</head>
<body>
<script language="javascript">
print_info();
</script>
</body>
</html>

frame4.html页面打开的时候,读取frame1.html里面变量thename和thefood的值
posted @ 2007-05-30 08:14  jhtchina  阅读(307)  评论(0)    收藏  举报