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) 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.


          var     new_window  =,   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+,
      var   wi   ='',  '_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 method.if the window was not opened by a script, the error will appear.



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

                     new_window = null;

      function    open_window(){
      var   features = "scrollbars=yes+,resizable=yes+,innerHeight=400+
 new_window   ='',  '_blank', features);
      function    close_window(){
                           if( new_window  && !new_window.closed ){

writing  to window

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

      function    write_window(){
      var   features = "scrollbars=yes+,resizable=yes+,innerHeight=400";
            features +=",innerWidth=400+,screenX=100+,screenY=40";
            new_window   =" ",  '_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='' target ='_blank' ";
 html_code +="style='font-size:20px;color:#3c0'></a></p>";