javascript当中open&close的用法


例 1.4.1(open&closeIEFF.html)




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<SCRIPT LANGUAGE="JavaScript">
<!--
function choice()
{
    /* -
- aNewWindow = open()
- aNewWindow = open(aURL)
- aNewWindow = open(aURL, aName)
- aNewWindow = open(aURL, aName, aFeatureList)
*/
    window.open("cla.html");
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<FORM METHOD=POST ACTION="" name="form1">
<INPUT TYPE="text" name="cla">
<INPUT TYPE="button" value="点我" onclick="choice()" >
</FORM>
</BODY>
</HTML>



cla.html



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<SCRIPT LANGUAGE="JavaScript">
<!--
function returnV(t)
{
/*
Window.opener (Property)
A reference to the window that contained the link that opened this one.
*/
    window.opener.document.form1.cla.value=t.value;
/* Window.close() (Method)
This will close the window.
JavaScript syntax: - close()
- myWindow.close()
The method window.close() will attempt to close the window in which the script is executing.
*/
    close();
}
//-->
</SCRIPT>
</HEAD>

<BODY>
<select onchange="returnV(this)">
    <option value="请选择班级">请选择班级</option>
    <option value="0708">0708</option>
    <option value="0709">0709</option>
    <option value="0802">0802</option>
</select>

</BODY>
</HTML>






例 1.4.2(OpenCloseWriteDocumentIEFF.html)



<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <script>
        var n = 0;
        var win = null;
        function show(msg)
        {
            /*Window.closed (Property)
             A property value that is true if the window is closed. qixy: but even though win is closed, win.closed=true, but
             win does not become null, because win is this window's property, it as a property is not destroyed.
             */
            msg=msg+"win is"+win;
            if ((win == null) || (win.closed))
            {

                msg=msg+"inside if (win == null) || (win.closed)";
                //               alert("win is"+win);

                /*aNewWindow = myWindow.open(aURL, aName, aFeatureList)
                 aFeatureList A list of attributes for the new window
                 aName The name of a new or existing target window
                 */
                win =window.open("","consoleqixy","width=400,height=250,resizable");
            }
            win.focus();
            /*马克-to-win:the following two statements means: clear the document. because clear is deprecated.if you remove the following two statements, win.document still has past content. so new content appends past content. so contens becomes more and more.qixy: if window is closed, its document also is cleared.*/
//            win.document.clear();
            win.document.close();
            win.document.open();
            win.document.writeln(msg);
            /*马克-to-win:the following two statements can not be used to write content, because later on the whole page content only will have the following two statement's content, so function body is also missing. to solve the problem refer to OpenCloseWriteDocument1.html, the conclusion is that when we want to write something, we open another new window to write in.*/

//document.writeln('<input type="button" value="单击" onclick="show(\''+ ++n+'\');">');
            //   document.writeln("this "+msg);
        }
    </script>
    <input type="button" value="单击" onclick="show('您单击了按钮:' + ++n + '次。');">







例 1.4.3(OpenCloseWriteDocument2IEFF.html)



<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <script>
        var n = 0;
        var win = null;
        function show(msg)
        {
            /*Window.closed (Property)
             A property value that is true if the window is closed.
             */
            if ((win == null) || (win.closed))
            {
                win =window.open("","consoleqixy","width=400,height=250,resizable");
                //将该文档打开成一个普通文档, 而不是一个text/html文档
                win.document.open("text/plain");
                win.x=9;
            }
            win.focus(  );
            win.document.writeln(msg+(++win.x));
        }
    </script>
    <input type="button" value="单击" onclick="show('您单击了按钮:' + ++n + '次。');">