在3秒后关闭弹出窗口

11

我需要在3秒后关闭以下弹出窗口。我该如何做?

<map id="ImgMap0" name="ImgMap0">
                  <area alt="" coords="127, 22, 20" alt="" title="click here" href="includes/popup1.htm" onclick="javascript:void window.open

('includes/popup1.htm','1366002941508','width=500,height=200,left=375,top=330');return false;" shape="circle" />
</map></p>

1
使用 setTimeout - Dom
可能是如何自动关闭网页的重复问题。 - davidcondrey
8个回答

16

可以使用setTimeout函数,例如:

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

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

示例代码: http://jsfiddle.net/N5rve/


14
<script type="text/javascript">
 function closeWindow() {
    setTimeout(function() {
    window.close();
    }, 3000);
    }

    window.onload = closeWindow();
    </script>

那应该就可以了。


只有在窗口打开后立即调用才能正常工作。它似乎不支持异步操作。 - darksoulsong

3
<area alt="" coords="127, 22, 20" alt="" title="click here" href="includes/popup1.htm" onclick="openWindow()" shape="circle" />

function openWindow(){
    var win = window.open('includes/popup1.htm', '1366002941508',  'width=500,height=200,left=375,top=330');
    setTimeout(function(){
        win.close()
    }, 3000);
    return false;
}

@leo_ap 这似乎是另一个答案的完全重复(https://dev59.com/jWQo5IYBdhLWcg3wV-Mx#16127137)。 - Mike Partridge
正是我所需要的。打开一个(弹出式)窗口,显示任何内容,并在3秒后关闭。谢谢@leo_ap - Markos F

1

尝试

<area alt="" coords="127, 22, 20" alt="" title="click here" href="includes/popup1.htm" onclick="openWindow()" shape="circle" />

function openWindow(){
    var win = window.open('includes/popup1.htm', '1366002941508',  'width=500,height=200,left=375,top=330');
    setTimeout(function(){
        win.close()
    }, 3000);
    return false;
}

Arun P Johny:完全没有起作用。这两个代码元素应该分开还是以某种方式合并?我是一个彻头彻尾的新手,所以你需要详细解释给我听。 - user964377

0

使用这些教程来获得你想要的内容。

http://www.tizag.com/javascriptT/javascriptredirect.php

<html>
  <head>
   <script type="text/javascript">
     function close_popup(){
        //code here...
     }
  </script>
 </head>
 <body onLoad="setTimeout('close_popup()', 3000)"> // 3000 milisec = 3sec

 </body>
</html>

0

在我们的代码中创建一个全局变量并重复使用它。

var info = function (text, onClose, headerText) {
              if (!headerText)
                headerText = "Info";
    
            alert(text, null, onClose, headerText, true);
    }
    
    // Call this in own code where ever you need
    
    info("Message that going to close automatic.");
    hidePopUpMessage();
    
    // callback function to showing 3 sec.
    function hidePopUpMessage() {
            setTimeout(function () {
                $("#pp-alert-close").click();
                //$("#popup-close").click();
            }, 3000);
        }


0

winpop = window.open("UnBlockPopUp.html");

if (!winpop || winpop.closed) alert("需要启用弹出窗口");

setTimeout(function() {

winpop.close()

}, 3000);


-3
<script type="text/javascript">
    function popup() {
        var myPopup = window.open('includes/popup1.htm','1366002941508','width=500,height=200,left=375,top=330');
        var t=setTimeout(myPopup.close(),3000);
        return false;
    }
</script>
<map id="ImgMap0" name="ImgMap0">
    <area alt="" coords="127, 22, 20" alt="" title="click here" href="includes/popup1.htm" onclick="popup();" shape="circle" />
</map>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接