简体   繁体   English

JavaScript中的window.close()函数

[英]window.close() function in javascript

I have a problem with window.close() function. 我对window.close()函数有问题。 I have a simple HTML script: 我有一个简单的HTML脚本:

<body>
<a href="http://www.gmail.com" class="one">gmail</a><br />
<a href="http://www.google.com" class="one">google</a><br />

<input type="submit" id="submit" name="submit" value="submit">

<script src="project.js"></script>
</body>

I want to open the above url's in different windows. 我想在不同的窗口中打开上面的URL。 Once the url's are loaded, the windows should close itself without any alerts. 加载网址后,窗口应自行关闭而不发出任何警报。 The below javascript code lets the url's open in different windows. 下面的javascript代码可让该网址在不同的窗口中打开。 But it closes only 'google.com' and the html webpage. 但它仅关闭“ google.com”和html网页。 The 'gmail' website remains open. “ gmail”网站保持打开状态。

var submit = document.getElementById("submit");

submit.onclick = function() {
    var getLinks = document.getElementsByClassName("one");

    for(var i=0; i<=(getLinks.length-1); i++) {
        window.open(getLinks[i]);
        getLinks[i].onload = window.close();
    }
}

What am I missing in this code. 我在这段代码中缺少什么。 Any help would be appreciated. 任何帮助,将不胜感激。 Note: the links are only for test. 注意:链接仅用于测试。

Check this out: 看一下这个:

function openWin() {
    myWindow = window.open("http://google.com", "myWindow", "width=200, height=100");    // Opens a new window
}

function closeWin() {
    myWindow.close();                                                  // Closes the new window
}

Easily found here: http://www.w3schools.com/jsref/met_win_close.asp 在这里容易找到: http : //www.w3schools.com/jsref/met_win_close.asp

Fiddle: http://jsfiddle.net/zc2g7nkr/ 小提琴: http : //jsfiddle.net/zc2g7nkr/

It works fine in Chrome and i do not have to allow to block pop-ups. 它在Chrome中正常运行,我不必阻止弹出窗口。

It's not possible any longer to close a window you have not created. 无法再关闭尚未创建的窗口。

Take a look at this window.close and self.close do not close the window in Chrome 看看这个window.close和self.close不会在Chrome中关闭该窗口

The problem is that certain browsers do not allow you to close a window using javascript. 问题是某些浏览器不允许您使用javascript关闭窗口。

There are bugs that prevent it - specifically that the browser won't let code from one website close a window that is opened to a different website. 有一些错误可以阻止它-特别是浏览器不允许来自一个网站的代码关闭打开到其他网站的窗口。

var win = window.open("http://www.google.com", '1366002941508','width=500,height=200,left=375,top=330');

setTimeout(function () { win.close();}, 3000);

Here is a DEMO 这是一个演示

It works fine in Firefox when you allowing popup, 当您允许弹出窗口时,它在Firefox中可以正常工作,

It opens a popup in chrome but it do not allow you to close a window using javascript. 它会在Chrome中打开一个弹出窗口,但不允许您使用javascript关闭窗口。

A page to B page A页到B页

window.open('B.html', '_blank');
window.open('','_self').close();

page close 页面关闭

window.open('','_self').close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM