Javascript-window Object Use Sample _ Basics

Source: Internet
Author: User
Tags object model
The Window object is the top-level object in the JavaScript browser object model, and contains several common methods and properties:

1 Open a new window
Copy Code code as follows:

window.open (Pageurl,name,parameters)

which

Pageurl as a child window path

Name is a child window handle

Parameters for window parameters (comma-separated arguments)

Such as:
Copy Code code as follows:

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 Open Mode window
Copy Code code as follows:

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

3 Close the window without ejecting the prompt box

If the Web page is not opened by a script program (window.open ()), the Window.close () script must first be set to NULL before it closes the window, otherwise the browser (IE7, IE8) will pop up a dialog box that determines the shutdown.
Copy Code code as follows:

<script language= "JavaScript" >
function CloseWindow ()
{
Window.opener = null;
window.open (', ' _self ', ');
Window.close ();
}
</script>
<input type= ' button ' value= ' close window ' onclick= ' CloseWindow () ' >
Or
<input type= "button" value= "Close Window" onclick= "window.opener = null;
window.open (', ' _self ', '); Window.close () ">

For closing a frame window
Copy Code code as follows:

<script language= "JavaScript" >
function CloseWindow ()
{
Window.opener = null;
window.open (', ' _top ', ');
Window.parent.close ();
}
</script>

4 Location objects using
Copy Code code as follows:

Window.location.reload ()//Refresh current page
Window.location.href= "http://www.cnblogs.com/zhouhb/"; Load other pages

5 History objects using
Copy Code code as follows:

Window.history.go (1); Forward
Window.history.go (-1); Back off

6 Subform to parent form value

6.1 Simple method

(1) Open the subform in the parent form
Copy Code code as follows:

var str=window.showmodaldialog ("s.html");
if (str!=null)
{
var V=document.getelementbyid ("V");
V.VALUE+=STR;
}

(2) Subform Code
Copy Code code as follows:

var V=document.getelementbyid ("V");
Window.parent.returnvalue=v.value;

Window.close ();

In addition, for ShowModalDialog open windows, you can also pass values through dialogarguments:

Parent Window Code:
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Untitled Document </title>
<script type= "Text/javascript" >
function OpenDialog ()
{
window.showModalDialog ("child.html", window, "Win", "Resable=false");
}
</script>

<body>
<form>
<input type= "text" id= "name"/>
<input type= "button" id= "Open" value= "open" onclick= "OpenDialog ()"/>
</form>
</body>

child Window Code:
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Untitled Document </title>
<script type= "Text/javascript" >
function Updateparent ()
{
var pwin=window.dialogarguments;//child window gets the passed parameter
if (pwin!=undefined)
{
Pwin.document.getElementById ("name"). Value=document.getelementbyid ("name"). Value;
}
}
</script>

<body>
<form>
<input type= "text" id= "name"/>
<input type= "button" id= "Update" value= "Update parent window" onclick= "updateparent ()"/>
</form>
</body>

For ShowModalDialog open windows, you can also pass values by Window.returnvalue:

Main window:
Copy Code code as follows:

<script type= "Text/javascript" >
function Openwindow () {
var bankcard=window.showmodaldialog ("counter.html", "" "," dialogwidth=400px;dialogheight=200px ");
Alert ("Your bank card password is" +bankcard+ ");
}
</SCRIPT>

(2) Open window
Copy Code code as follows:

<HEAD>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<TITLE> Window Practice </TITLE>
<script type= "Text/javascript" language= "JavaScript" >
function CloseWindow () {
Window.returnvalue=document.myform.cardpass.value;
Window.close ();
}
</SCRIPT>
</HEAD>
<BODY>
<form name= "MyForm" action= "" method= "POST" >
Account Information:<br>
Please keep your account information in case of loss <BR>
Account Number: <input name= "Cardnum" type= "text" size= "><BR>"
Password: <input name= "Cardpass" type= "password" size= "><BR>"
<input type= "button" Name= "BTN" value= "confirmation" onclick= "CloseWindow ()" >
</FORM>
</BODY>

6.2 More detailed Introduction

It is well known that the window.open () function can be used to open a new window, so how to pass a value to the parent form in the subform, in fact, by Window.opener to get a reference to the parent form.
If we create a new form fatherpage.htm:
Copy Code code as follows:

<script type= "Text/javascript" >
function Openchildwindow ()
{
window.open (' childpage.htm ');
}
</script>
<input type= "text" id= "Txtinput"/>
<input type= "button" value= "Openchild" onclick= "Openchildwindow ()"/>

The elements in the parent form can then be accessed through Window.opener in childpage.htm:
Copy Code code as follows:

<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 ()"/>

In fact, when you open a subform, we can also assign the elements of the subform, because the window.open function also returns a reference to a subform, so fatherpage.htm can be modified to:
Copy Code code as follows:

<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 ()"/>

By determining whether the reference to the subform is empty, we can also control that it can only open one subform:
Copy Code code as follows:

<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 ()"/>

The light is not enough, when you close the subform, you must also empty the child variable for the parent form, otherwise, when you open the subform and then close it, you cannot reopen it again:
Copy Code code as follows:

<body onunload= "Unload ()" >
<script type= "Text/javascript" >
Function SetValue ()
{
Window.opener.document.getElementById (' Txtinput '). Value
=document.getel Ementbyid (' Txtinput '). Value;
Window.close ();
}
Function Unload ()
{
Window.opener.child=null;
}
</script>
<input type= "text" id= "Txtinput"/>
<input type= "button" value= "Setfather "Onclick=" SetValue () "/>
</body>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.