How can I close a browser window without receiving

2019-01-01 00:37发布

How can I close a browser window without receiving the Do you want to close this window prompt?

The prompt occurs when I use the window.close(); function.

18条回答
看风景的人
2楼-- · 2019-01-01 01:07

Create a JavaScript function

<script type="text/javascript">
    function closeme() {
        window.open('', '_self', '');
        window.close();
    }
</script>

Now write this code and call the above JavaScript function

<a href="Help.aspx" target="_blank" onclick="closeme();">Help</a>

Or simply:

<a href="" onclick="closeme();">close</a>
查看更多
冷夜・残月
3楼-- · 2019-01-01 01:09
window.open('', '_self', ''); window.close();

This works for me.

查看更多
浮光初槿花落
4楼-- · 2019-01-01 01:12

I am going to post this because this is what I am currently using for my site and it works in both Google Chrome and IE 10 without receiving any popup messages:

<html>
    <head>
    </head>
    <body onload="window.close();">
    </body>
</html>

I have a function on my site that I want to run to save an on/off variable to session without directly going to a new page so I just open a tiny popup webpage. That webpage then closes itself immediately with the onload="window.close();" function.

查看更多
步步皆殇っ
5楼-- · 2019-01-01 01:13

In the body tag:

<body onload="window.open('', '_self', '');">

To close the window:

<a href="javascript:window.close();">

Tested on Safari 4.0.5, FF for Mac 3.6, IE 8.0, and FF for Windows 3.5

查看更多
时光乱了年华
6楼-- · 2019-01-01 01:13

Because of the security enhancements in IE, you can't close a window unless it is opened by a script. So the way around this will be to let the browser think that this page is opened using a script, and then to close the window. Below is the implementation.

Try this, it works like a charm!
javascript close current window without prompt IE

<script type="text/javascript">
function closeWP() {
 var Browser = navigator.appName;
 var indexB = Browser.indexOf('Explorer');

 if (indexB > 0) {
    var indexV = navigator.userAgent.indexOf('MSIE') + 5;
    var Version = navigator.userAgent.substring(indexV, indexV + 1);

    if (Version >= 7) {
        window.open('', '_self', '');
        window.close();
    }
    else if (Version == 6) {
        window.opener = null;
        window.close();
    }
    else {
        window.opener = '';
        window.close();
    }

 }
else {
    window.close();
 }
}
</script>

javascript close current window without prompt IE

查看更多
听够珍惜
7楼-- · 2019-01-01 01:16

In my situation the following code was embedded into a php file.

var PreventExitPop = true;
function ExitPop() {
  if (PreventExitPop != false) {
    return "Hold your horses! \n\nTake the time to reserve your place.Registrations might become paid or closed completely to newcomers!"
  }
}
window.onbeforeunload = ExitPop;

So I opened the console and write the following

PreventExitPop = false

This solved the problem. So, find out the JavaScript code and find the variable(s) and assign them to an appropriate "value" which in my case was "false"

查看更多
登录 后发表回答