最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript-how to open window (from hyperlink) and then close it with delay of 5 sec? - Stack Overflow

programmeradmin0浏览0评论

I am trying to open new window from hyperlink using java script and then auto close it in five seconds. It either closes right away or doesn't close at all. Here are some samples of code I was using:

"function closeOnLoad(myLink){var newWindow=window.open(myLink);newWindow.onload=SetTimeout(newWindow.close(),5000);}" + LinkText + ""

I am trying to open new window from hyperlink using java script and then auto close it in five seconds. It either closes right away or doesn't close at all. Here are some samples of code I was using:

"function closeOnLoad(myLink){var newWindow=window.open(myLink);newWindow.onload=SetTimeout(newWindow.close(),5000);}" + LinkText + ""

Share Improve this question asked Sep 22, 2009 at 21:23 aleks2009aleks2009 231 silver badge4 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 9

You're better off closing the window from the parent instead of defining an onload handler within the child. Due to security restrictions, you simply may not have access to modify child window events.

function closeOnLoad(myLink)
{
  var newWindow = window.open(myLink);
  setTimeout(
             function()
             {
               newWindow.close();
             },
             5000
            );
  };
}

you need to use what is called a 'closure' to wrap the timeout in. It's like the function to timeout and then close is wrapped within another function.

I won't go into detail here, but lookup javascript and closures and play around to see how they work.

Here's a link to help get you started: http://www.jibbering./faq/faq_notes/closures.html

The window closing code should be in the window's code:

$(document).ready(function() {
    setTimeout(function() {
        window.close();
    },5000);
})

BUT, you will get a popup asking for the user to confirm if you try & close the popup that way.

To unload is the unload() function. Here you have an example.

发布评论

评论列表(0)

  1. 暂无评论