Javascript-window Object Usage Example

Source: Internet
Author: User

The

 window object is the top-level object in the JavaScript browser object model that contains several commonly used methods and properties, and here is a description of the use of the next Window object

The Window object is the top-level object in the JavaScript browser object model, containing several common methods and properties:    1 opens a new window   code as follows: window.open (pageurl,name,parameters)     where:    pageurl for child window path     name for child window handle     parameters for window parameters (comma separated parameters)     such as:  code are 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     code as follows: window.showModalDialog ("http://www.cnblogs.com/zhouhb/", "open", "toolbars=0;width=200;height=200");    3 Close the window without pop-up balloon     If the page is not opened via a script (window.open ()), the Window.close () script must be set to null before the window is closed, otherwise the browser (IE7, IE8) pops up a dialog box that is OK to close.     Code as follows: <script language= "JavaScript" >  function CloseWindow ()   {  Window.opener = null ;  window.open (', ' _self ', ');  window.close (); }  </script>  <input type= ' Button ' value= ' closes window ' onclick= ' CloseWindow () ' >    <input type= "button" value= "Close Window" onclick= "Window.opener = null;  window.open (', ' _self ', '"); Window.close () " >    for closing the frame window   code is as follows: <script language= "JavaScript" >  function CloseWindow ()   {  Window.opener = null;  window.open (', ' _top ', ');  window.parent.close (); }  </script>     4 Location object using   code as follows: Window.location.reload ()//Refresh current page   window.location.href= "http:// www.cnblogs.com/zhouhb/"; Load other pages     5 History object using   code as follows: Window.history.go (1); Forward   Window.history.go (-1); Back     6 subform to parent form     6.1 simple method     (1) Open the subform in the parent form the   code is as follows: Var str=window.showmodaldialog ("s.html");  if (str!=null)   {  var V=document.getelementbyid ("V");  v.value+=str; -}  & nbsp (2) Subform code   code as follows: Var V=document.getelementbyid ("V");  window.parent.returnvalue=v.value;    Window.close ();    In addition, for ShowModalDialog open windows, alsoYou can pass the Dialogarguments value:    parent window code:  code as follows: <! 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>  <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"); This uses window objects as parameter passes  }  </script>  </head>    <body>  <form>   <input type= "text" id= "name"/>  <input type= "button" id= "Open" value= "open" onclick= "OpenDialog () "/>  </form>  </body>  </html>    child window code:  code is as follows: <! 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>  <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>  </head>    <body>  <form>  <input type= "text" id= "name "/>  <input type= button" id= "Update" value= "Updating parent window" onclick= "Updateparent ()"/>  </form>   </body>  </html>    for ShowModalDialog open windows, you can also pass Window.returnvalue values:    main Window:    code is 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+ "n"); }  </SCRIPT>    (2) Opens the window   code as follows: <head>& nbsp <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 to avoid 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= "Confirm" onclick= "CloseWindow ()" >  </FORM>  </ body>    6.2 More details     well-known 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, you can get a reference to the parent form by Window.opener.   As we create a new form FatherPage.htm:    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:  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 the 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:  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 a subform:  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 "button" type= "value=" Onclick= "Openchildwindow ()"/>    Light This is not enough, when you close the subform, you must also empty the child variable for the parent form, or you can reopen the subform and then close it before you reopen the:  code as follows: <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 "button" type= "value=" Onclick= "SetValue ()"/>  </body> 

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.