3秒后关闭弹出窗口

时间:2013-04-21 01:37:39

标签: javascript html html5

我需要在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>

9 个答案:

答案 0 :(得分:14)

使用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/

答案 1 :(得分:9)

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

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

应该这样做。

答案 2 :(得分:2)

这是一个关于倒计时3秒后JavaScript自动弹出窗口关闭的示例,

<p style="text-align:center">This window will close automatically within <span id="counter">3</span> second(s).</p>
<script type="text/javascript">



 function countdown() {

    var i = document.getElementById('counter');

    i.innerHTML = parseInt(i.innerHTML)-1;

 if (parseInt(i.innerHTML)<=0) {

  window.close();

 }

}

setInterval(function(){ countdown(); },1000);

</script>

我找到了它here。希望这对你有所帮助。

答案 3 :(得分: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;
}

答案 4 :(得分: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;
}

答案 5 :(得分: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>

答案 6 :(得分:0)

  #For example    
  <div class="popup" onclick="myFunction()">
    <span class="popuptext" id="myPopup">APopup text</span>
 </div>
 function myFunction() {
  var popup = document.getElementById("myPopup");
  popup.classList.toggle("show");
  if(popup.classList.contains("show")) 
  setTimeout(() => popup.classList.remove("show"), 300) 

}

答案 7 :(得分: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);
        }

答案 8 :(得分:-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>