0
点赞
收藏
分享

微信扫一扫

window.open()方法(弹出层)

1, 最基本的弹出窗口代码

window.open('page.html');


2, 经过设置后的弹出窗口

window.open('page.html', 'newwindow', 'height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no') //该句写成一行代码

参数解释:

window.open 弹出新窗口的命令;

  'page.html' 弹出窗口的文件名;

  'newwindow' 弹出窗口的名字(不是文件名),非必须,可用空''代替;

  height=100 窗口高度;

  width=400 窗口宽度;

  top=0 窗口距离屏幕上方的象素值;

  left=0 窗口距离屏幕左侧的象素值;

  toolbar=no 是否显示工具栏,yes为显示;

  menubar,scrollbars 表示菜单栏和滚动栏。

  resizable=no 是否允许改变窗口大小,yes为允许;

  location=no 是否显示地址栏,yes为允许;

  status=no 是否显示状态栏内的信息(通常是文件已经打开),yes为允许;


3, 用函数控制弹出窗口

下面是一段完整的代码

<html>

  <head>

  <script LANGUAGE="JavaScript">

  <!--

  function openwin() {

  window.open ("page.html", "newwindow", "height=100, width=400, toolbar =no, menubar=no, scrollbars=no, resizable=no, location=no, status=no") //写成一行

  }

  //-->

  </script>

  </head>

  <body οnlοad="openwin()">

  任意的页面内容...

  </body>

  </html>

解释:这里定义了一个函数openwin(), 函数内容就是打开一个窗口。在调用它之前没有任何用途。怎么调用呢?



方法一:<body οnlοad="openwin()"> 浏览器读页面时弹出窗口;

方法二:<body οnunlοad="openwin()"> 浏览器离开页面时弹出窗口;

方法三:用一个连接调用:

<a href="#" οnclick="openwin()"> 打开一个窗口</a>

注意:使用的"#"是虚连接。

方法四:用一个按扭调用:

<input type="button" οnclick="openwin()" value="打开窗口" />


4, 弹出两个窗口

对代码稍微改动如下:

 <script LANGUAGE="JavaScript">

  <!--

  function openwin() {

  window.open ("page.html", "newwindow", "height=100, width=100, top=0, left=0,toolbar=no, menubar=no, scrollbars=no, resizable=no, location=n o, status=no")//写成一行

  window.open ("page2.html", "newwindow2", "height=100, width=100, top=100, left=100,toolbar=no, menubar=no, scrollbars=no, resizable=no, loca tion=no, status=no")//写成一行

  }

  //-->

  </script>

为避免弹出的2个窗口覆盖,用top和left控制一下弹出的位置不要相互覆盖即可。最后用上面的说过的四种方法调用即可。

注意:2个窗口的name(newwindow与 newwindow2)不要相同,或者干脆全部为空。


5, 主窗口打开文件1.htm,同时弹出小窗口page.html

如下代码加入主窗口<head>区:

function openwin()

{

window.open("page.html","","width=200,height=200")

}

加入body区:

<a href="1.htm" οnclick="openwin()">open</a>即可。


6, 弹出的窗口之定时关闭控制

将一小段代码加入弹出的页面(注意是加入page.html的HTML中,可不是主页面中,否则......),让它在10秒后自动关闭是不是更酷了?

function closeit()

{

setTimeout("selft.close()", 10000) //毫秒

}

然后,在body 中添加:<body οnlοad="closeit()">即可。


7, 在弹出窗口中加上一个关闭按扭

<input type="button" value="关闭" οnclick="window.close()">


8, 内包含的弹出窗口---一个页面两个窗口

上面的例子都包含两个窗口,一个是主窗口,另一个是弹出的小窗口。通过下面的例子,你可以在一个页面内完成上面的效果。
  <html>
  <head>
  <SCRIPT LANGUAGE="JavaScript">
  function openwin()
  {
  OpenWindow=window.open("", "newwin", "height=250, width=250,toolbar=no ,scrollbars="+scroll+",menubar=no");
  //写成一行
  OpenWindow.document.write("<TITLE>例子</TITLE>")
  OpenWindow.document.write("<BODY BGCOLOR=#ffffff>")
  OpenWindow.document.write("<h1>Hello!</h1>")
  OpenWindow.document.write("New window opened!")
  OpenWindow.document.write("</BODY>")
  OpenWindow.document.write("</HTML>")
  OpenWindow.document.close()
  }
  </SCRIPT>
  </head>
  <body>
  <a href="#" οnclick="openwin()">打开一个窗口</a>
  <input type="button" οnclick="openwin()" value="打开窗口">
  </body>
  </html>
OpenWindow.document.write()里面的代码不就是标准的HTML吗?只要按照 格式写更多的行即可。千万注意多一个标签或少一个标签就会出现错误。记得用 OpenWindow.document.close()结束啊。

9, 终极应用---弹出的窗口这Cookie控制
回想一下,上面的弹出窗口虽然酷,但是有一点小毛病,比如你将上面的脚本放在一个需要频繁经过的页面里(例如首页),那么每次刷新这个页面,窗口都会弹出一次,是不是非常烦人?
有解决的办法吗?我们使用cookie来控制即可。
首先,将如下代码加入主页面的Html的head区:
  function openwin(){
  window.open("page.html","","width=200,height=200")
  }
function get_cookie(Name)
{
var search=Name+"=";
var returnvalue="";
if(document.cookie.length>0){
   if (offset != -1) {
   offset += search.length
   end = document.cookie.indexOf(";", offset);
   if (end == -1)
   end = document.cookie.length;
   returnvalue=unescape(document.cookie.substring(offset, end));
}
}
return returnvalue;
}
function ladpopup()
{
if(get_cookie('popped=yes'))
{
openwin()
document.cookie="popped=yes";
}
}
最后,用<body οnlοad="loadpopup()"> 父级.aspx代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="弹出窗口.aspx.cs" Inherits="弹出窗口" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

</head>
<body οnbefοreunlοad="return confirm('确定要离开吗?')">
<form id="form1" runat="server">
<div>

<input id="Button1" type="button" value="button" οnclick="window.open('ChildWin.aspx','弹出框','width=900,height=450,top=300,left=300');"/></div>
</form>
</body>
</html>
ChildWin.aspx代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ChildWin.aspx.cs" Inherits="ChildWin" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:BulletedList ID="BulletedList1" runat="server" Width="526px">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
</asp:BulletedList>

<br />
<input id="Button1" type="button" value="关闭" OnClick="window.close();"/></div>
</form>
</body>
</html>

举报

相关推荐

0 条评论