javaScript windows open close methods

the window object methods open() and close() are used to create and destroy a window, respectively.


JavaScript - window open()  method

the javascript(bom) window.open() method opens a new window and loads the document specified by a given url(uniform resource locator).

by default, the open method creates a window that has a default width and height and the standard menu, toolbar, and other features of browser window. you can alter this set of features by using the features parameter.

syntax:

          var     new_window  =  window.open(url,   name,    features,  replace) ;
  • url : the url to be loaded in the newly opened window
  • name: string name is used as the value for the target attribute on a form or an anchor.
  • features : optional features(size, position,width, height,menu bar etc) of the new window
  • replace : if value 'true' url replaces the current document in the history list.if value 'false'                 url creates a new entry in the history list.
      function    open_window(){
      
   
      var   features = "scrollbars=yes+,resizable=yes+,innerHeight=400+,
       
                                           innerWidth=800+,screenX=200+,screenY=400";
                        
      var   wi   =  window.open('http://www.google.co.in/',  '_blank', features);
      
              
                        }

JavaScript - window close()  method

the window.close() method closes the current browser window or html application.

note : this method is only allowed to be called for windows that were opened by a script using the window.open method.if the window was not opened by a script, the error will appear.

syntax:

          window.close();

the closed property returns a boolean value indicating whether a window has been closed or not.

   <script>
                     new_window = null;

      function    open_window(){
      
   
      var   features = "scrollbars=yes+,resizable=yes+,innerHeight=400+
       
                                           innerWidth=800+,screenX=200+,screenY=400";
                        
 new_window   =  window.open('http://www.google.co.in/',  '_blank', features);
      
                        
                        }
                        
                        
      function    close_window(){
     
                           if( new_window  && !new_window.closed ){
                           
                                      new_window.close();
                                      
                                    }                    
                        
                              }
    </script>                              
                        

writing  to window

the document.write() method writes javaScript code or html expressions to a document.

   <script>
             
      function    write_window(){
      
   
      var   features = "scrollbars=yes+,resizable=yes+,innerHeight=400";
       
            features +=",innerWidth=400+,screenX=100+,screenY=40";
                        
            new_window   =  window.open(" ",  '_blank', features);
      
 var  html_code = "<html><head><title> this is demo for window how to write";
            
 html_code +="</title></head><body>";
 
 html_code +="<h1 style='color:#3a4b9c;'>this is the first heading</h1>";
                 
 html_code +="<p><img  src='/project/images/goldenTree.jpg'/></p>";
                 
 html_code +="<p><a   href='http://google.com/' target ='_blank' ";
 
 html_code +="style='font-size:20px;color:#3c0'>google.com</a></p>";                     
                        
           new_window.document.write(html_code);       
                        
         }                       
 </script>