• 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 onload="openwin()"> 
      随意的页面内容... 
      </body> 
      </html> 
       解释:这里定义了一个函数openwin(), 函数内容就是打开一个窗体。

    在调用它之前没有不论什么用途。怎么调用呢?
       
       方法一:<body onload="openwin()"> 浏览器读页面时弹出窗体;
       方法二:<body onunload="openwin()"> 浏览器离开页面时弹出窗体;
       方法三:用一个连接调用:
                  <a href="#" onclick="openwin()"> 打开一个窗体</a>
                  注意:使用的"#"是虚连接。
       方法四:用一个按扭调用:
                   <input type="button" onclick="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" onclick="openwin()">open</a>就可以。

    6, 弹出的窗体之定时关闭控制
        将一小段代码增加弹出的页面(注意是增加page.html的HTML中。可不是主页面中。否则......),让它在10秒后自己主动关闭是不是更酷了?
        function closeit()
        {
        setTimeout("selft.close()", 10000)   //毫秒
        }
        然后。在body 中加入:<body onload="closeit()">就可以。

    7, 在弹出窗体中加上一个关闭按扭
        <input type="button" value="关闭" onclick="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="#" onclick="openwin()">打开一个窗体</a> 
      <input type="button" onclick="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 onload="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 onbeforeunload="return confirm('确定要离开吗?')">
        <form id="form1" runat="server">
        <div>
        
            <input id="Button1" type="button" value="button" onclick="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>
    



  • 相关阅读:
    [原创]手把手教你如何把二维码插件zxing加入到android和ios项目中
    解决通过Intent调用系统拍照程序,返回图片太小的问题[android] 【转】
    SVN Command
    取得ie 里面 自定义函数或者属性的集合 使用RuntimeObject()
    scrum 开发模型
    javascript AOP 实现,ajax回调函数使用比较方便
    印度英语的特点
    AspectJS
    java 打jar包 转
    XP 开发模式
  • 原文地址:https://www.cnblogs.com/yangykaifa/p/7125331.html
Copyright © 2020-2023  润新知