• window对象使用


    window对象是JavaScript浏览器对象模型中的顶层对象,包含多个常用方法和属性:

    1 打开新窗口

    window.open(pageURL,name,parameters)

    其中:

    pageURL为子窗口路径

    name为子窗口句柄

    parameters为窗口参数(各参数用逗号分隔)

         如:window.open("http://www.cnblogs.com/zhouhb/","open",

    'height=100,width=400,top=0,left=0,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');

    2 打开模式窗口

          window.showModalDialog("http://www.cnblogs.com/zhouhb/","open","toolbars=0;width=200;height=200");

    3 关闭窗口

            window.opener=null;   //不弹出提示框
            window.close();

    4 location对象使用

          window.location.reload();//刷新当前页

          window.location.href="http://www.cnblogs.com/zhouhb/";  //载入其他页面

    5 history对象使用

        window.history.go(1);  //前进

         window.history.go(-1);  //后退

    6 子窗体向父窗体传值

    6.1 简单方法  

    (1)在父窗体中打开子窗体

             var str=window.showModalDialog("s.html");
             if(str!=null) 
            {             
               var v=document.getElementById("v");
               v.value+=str;        
            }

    (2)子窗体代码

        var v=document.getElementById("v");
        window.parent.returnValue=v.value;

        window.close();

     6.2 更加详细的介绍

        众所周知window.open() 函数可以用来打开一个新窗口,那么如何在子窗体中向父窗体传值呢,其实通过window.opener即可获取父窗体的引用。
    如我们新建窗体FatherPage.htm:
    <script type="text/javascript">
    function OpenChildWindow()
    {
        window.open('ChildPage.htm');  
    }
    </script>
    <input type="text" id="txtInput" />
    <input type="button" value="OpenChild" onclick="OpenChildWindow()" />

    然后在ChildPage.htm中即可通过window.opener来访问父窗体中的元素:
    <script type="text/javascript">
    function SetValue()
    {
        window.opener.document.getElementById('txtInput').value
            =document.getElementById('txtInput').value;
        window.close();
    }
    </script>
    <input type="text" id="txtInput" />
    <input type="button" value="SetFather" onclick="SetValue()" />

    其实在打开子窗体的同时,我们也可以对子窗体的元素进行赋值,因为window.open函数同样会返回一个子窗体的引用,因此FatherPage.htm可以修改为:
    <script type="text/javascript">
    function OpenChildWindow()
    {
        var child = window.open('ChildPage.htm'); 
        child.document.getElementById('txtInput').value
            =document.getElementById('txtInput').value;
    }
    </script>
    <input type="text" id="txtInput" />
    <input type="button" value="OpenChild" onclick="OpenChildWindow()" />

    通过判断子窗体的引用是否为空,我们还可以控制使其只能打开一个子窗体:

    <script type="text/javascript">
    var child
    function OpenChildWindow()
    {
        if(!child)   
            child = window.open('ChildPage.htm'); 
         child.document.getElementById('txtInput').value
                =document.getElementById('txtInput').value;
    }
    </script>
    <input type="text" id="txtInput" />
    <input type="button" value="OpenChild" onclick="OpenChildWindow()" />

    光这样还不够,当关闭子窗体时还必须对父窗体的child变量进行清空,否则打开子窗体后再关闭就无法再重新打开了:
    <body onunload="Unload()">
    <script type="text/javascript">
    function SetValue()
    {
        window.opener.document.getElementById('txtInput').value
            =document.getElementById('txtInput').value;
        window.close();
    }
    function Unload()
    {
        window.opener.child=null;
    }
    </script>
    <input type="text" id="txtInput" />
    <input type="button" value="SetFather" onclick="SetValue()" />
    </body>

  • 相关阅读:
    Linux下tar-rar-unrar解压/压缩缩命令大全
    Linux文件和目录权限详细讲解
    Mac版PhpStorm之XAMPP整合apache服务器配置
    android adb pull push
    Android刷机教程之LG Nexus 5X线刷官方Nexus系列教程
    Android RecyclerView.Adapter notifyDataSetChanged 不起作用
    Android Gradle Build Error:Some file crunching failed, see logs for details解决办法
    Mac平台与Windows平台下AndroidStudio增量升级
    Android与JS之间跨平台异步调用
    Android Studio 打包签名发布New Key Store
  • 原文地址:https://www.cnblogs.com/chengpeng/p/2150359.html
Copyright © 2020-2023  润新知