Home>

window.close ();A summary of the JavaScript code for closing the browser window

Serial number Close code need confirmation No effect No need to confirm test
1 window.close () ie7 firefox, chrome, safari opera close
2 window.opener=null;window.open ("", "_ self");window.close (); firefox ie7, opera, chrome, safari close
3 window.open ("", "_ self");window.close (); firefox ie7, opera, chrome, safari close
4 window.opener=null;window.close (); ie7 firefox, safari chrome, opera close
5 var opened=window.open ("about:blank", "_ self");opened.opener=null;opened.close (); firefox safari, ie7, chrome, opera close
6 var opened=window.open ("about:blank", "_ self");opened.close (); safari, firefox firefox, ie7, chrome, opera close

Open a page in many ways,Then close it with window.close (), which behaves differently in each browser.For example, when you enter the url directly in the address bar, calling window.close () under firefox chrome safari to close the page is invalid.Another example is ctrl + click the link to open the window,Firefox cannot be closed by calling window.close ().

The differences are as follows:

URL in the address bar click the link ctrl + click link window.open window.showmodaldialog
ie6 Prompt window closed Close directly Close directly Close directly Close directly
ie7/8 Close directly Prompt window closed Prompt window closed Close directly Close directly
firefox Not closed, no prompt Close directly Not closed, no prompt Close directly Close directly
safari Not closed, no prompt Close directly Close directly Close directly Close directly
chrome Not closed, no prompt Close directly Close directly Close directly Close directly
opera Close directly Close directly Close directly Close directly not support

Possible reasons for unable to close under firefox:

It's not a problem of window.close () of js code, but a configuration problem of firefoxThe solution is as follows:Type about:config in the address bar of firefox. Find dom.allow_scripts_to_close_windows in the configuration list. Right-click on the selection and change the above false to true. Note:The default is false, to prevent the script from closing the window randomly

Attached code:window.opener=null;//To prevent the prompt window window.close ();//Close the window

  • Previous Access to SQL database method
  • Next Detailed jQuery extend () and simple examples