Javascript中打开窗口、聚焦窗口、关闭窗口攻略

该博客主要介绍了使用JavaScript实现窗口操作的方法。通过定义info、closeNewWindow和focusNewWindow三个函数,分别实现打开、关闭和聚焦窗口的功能,并在HTML的表单中添加按钮,通过onClick事件调用这些函数来触发相应操作。

<script>
var info,infotext,newWindow;
function info()
{
  newWindow=window.open("打开的窗口的URL","infoWin","height=260,width=350,scrollbars=no,resizable=no");
}

function closeNewWindow()
{
  if(newWindow)
  {
    if(!newWindow.closed)
      newWindow.close()
    else alert(" 窗口已经关闭了!")
  }
  else
  {
    alert ("你尚未打开一个窗口!/n只有打开窗口才能关闭呀!")
  }
}
function focusNewWindow()
{
  if (newWindow)
  {
    if(!newWindow.closed)
      newWindow.focus()
    else alert(" 窗口已经关闭了!")
  }
  else
  {
    alert ("我无法聚焦到某个窗口上/n可能是因为窗口没有打开吧!")
  }
}

</script>

2.在<body></body>中加入下列代码。

<form>
<input type="button" value="打开窗口" onClick="info()">
<input type="button" value="关闭窗口" onClick="closeNewWindow()">
<input type="button" value="聚焦窗口" onClick="focusNewWindow()">
</form>
 
 
<table border="0" width="75%" cellspacing="0" cellpadding="5">
<tr>
<form>
<td height="112">
<div align="center">
<center>
<p>
<input onClick="info()" type="button" value="打开窗口" name="button">
<input onClick="closeNewWindow()" type="button" value="关闭窗口" name="button2">
<input onClick="focusNewWindow()" type="button" value="聚焦窗口" name="button2">
</p>
</center>
</div>
</form>
<p>

<script>
var info,infotext,newWindow;
function info()
{
  newWindow=window.open("打开的窗口的URL","infoWin", "height=260,width=350,scrollbars=no,resizable=no");<br>
}
function closeNewWindow()
{
  if (newWindow)
  {
    if(!newWindow.closed)
      newWindow.close()
    else alert(" 窗口已经关闭了!")
  }
  else
  {
    alert ("你尚未打开一个窗口!/n只有打开窗口才能关闭呀!")
  }
}
function focusNewWindow()
{
  if (newWindow)
  {
    if(!newWindow.closed)
       newWindow.focus()
    else alert(" 窗口已经关闭了!")
  }
  else
  {
    alert ("我无法聚焦到某个窗口上/n可能是因为窗口没有打开吧!")
  }
}
</script>

2.在<body>与</body>中加入下列代码。
<form>
<input type="button" value="打开窗口" onClick="info()">
<input type="button" value="关闭窗口" onClick="closeNewWindow()">
<input type="button" value="聚焦窗口" onClick="focusNewWindow()">
</form>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值