JavaScript——window对象

本节目标:

    掌握window对象的操作方法

window对象

在使用JS打开窗口或关闭窗口时,或者做一些比较特殊的效果时,需要使用window对象。

<html lang="en">
 <head>
  <meta charset="UTF-8">
  <script type="text/javascript">
function fun(thisurl){
window.open(thisurl);
}
  </script>
 </head>
 <body οnlοad="fun('test.html')">
 </body>

</html>

如上,在加载一个页面时,打开另一个窗口。

打开新窗口操作:

<html lang="en">
 <head>
  <meta charset="UTF-8">
  <script type="text/javascript">
function fun(thisurl){
window.open(thisurl,"页面标题","width=470,heigth=150,scrollbars=yes,resizeable=no");
        }
  </script>
 </head>
 <body>
<form method="post" action="" name="myForm">
网址:<select name="url" οnchange="fun(this.value)">
<option value="test1.html">test1</option>
<option value="test2.html">test2</option>
</select>
</form>
 </body>

</html>

提示确认操作:

<html lang="en">
 <head>
  <meta charset="UTF-8">
  <script type="text/javascript">
function fun(){
if(window.confirm("确定删除?")){
alert("删除成功");
}else{
alert("取消");
}
}
  </script>
 </head>
 <body>
<a href="#" οnclick="fun()">删除邮件</a>
 </body>

</html>

页面重定向操作:

<html lang="en">
 <head>
  <meta charset="UTF-8">
  <script type="text/javascript">
function fun(thisurl){
window.location=thisurl;
}
  </script>
 </head>
 <body>
<form method="post" action="" name="myForm">
<select οnchange="fun(this.value)">
<option value="#">===请选择===</option>
<option value="test1.html">test1.html</option>
<option value="test2.html">test2.html</option>
</select>
</form>
 </body>

</html>

在window对象之间也存在父子关系,通过一个窗口打开另一个窗口,那么原本的窗口被称为父窗口,被打开的窗口称为子窗口。


父窗口:

<html lang="en">
 <head>
  <meta charset="UTF-8">
  <script type="text/javascript">
function fun(thisurl){
window.open(thisurl,"页面标题","width=470,heigth=150,scrollbars=yes,resizeable=no");
}
  </script>
 </head>
 <body>
<form method="post" action="" name="parentForm">
<input type="button" value="打开" onClick="fun('test1.html')"><br>
</form>
 </body>
</html>

子窗口test1.html:

<html lang="en">

 <head>
  <meta charset="UTF-8">
  <script type="text/javascript">
function closewin(){
window.close();
}
window.opener.reload();//刷新页面
  </script>
 </head>
 <body>
  <a href="" οnclick="closewin()">关闭窗口</a>
 </body>
</html>

从子窗口中接收数据到父窗口中:

父窗口:

<html lang="en">
 <head>
  <meta charset="UTF-8">
  <script type="text/javascript">
function fun(thisurl){
window.open(thisurl,"页面标题","width=470,heigth=150,scrollbars=yes,resizeable=no");
}
  </script>
 </head>
 <body>
<form method="post" action="" name="parentForm">
<input type="button" value="打开" onClick="fun('test1.html')"><br>
选择的结果是:<input type="text" name="result">
</form>
 </body>
</html>

子窗口:

<html lang="en">
 <head>
  <meta charset="UTF-8">
  <script type="text/javascript">
function returnVal(){
var city = document.myForm.city.value;//取出当前选择的信息
var doc = window.opener.document;//取得父窗口文档
doc.parentForm.result.value = city;//设置新内容
window.close();
}
  </script>
 </head>
 <body>
  <form method="post" action="" name="myForm">
选择:<select name="city">
<option value="#">===请选择===</option>
<option value="北京">北京</option>
<option value="上海">上海</option>
<option value="杭州">杭州</option>
  </select>
  <input type="button" value="返回" οnclick="returnVal()">
  </form>
 </body>
</html>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值