window.close() doesn't work - Scripts may not close windows that were not opened by script.

Summary

Closes the current window, or a referenced window.

Syntax

window.close();

Description

When this method is called, the referenced window is closed.

This method is only allowed to be called for windows that were opened by a script using thewindow.open method. If the window was not opened by a script, the following error appears in the JavaScript Console: Scripts may not close windows that were not opened by script.

Examples

Closing a window opened with window.open()

<script type="text/javascript">
//Global var to store a reference to the opened window
var openedWindow;

function openWindow()
{
  openedWindow = window.open('moreinfo.htm');
}
function closeOpenedWindow()
{
  openedWindow.close();
}
</script>

Closing the current window

<script type="text/javascript">
function closeCurrentWindow()
{
  window.close();
}
</script>
发布了430 篇原创文章 · 获赞 415 · 访问量 925万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览