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

javascript - Is window.open() still useful nowadays? - Stack Overflow

programmeradmin2浏览0评论

I am learning JS and stumbled across the window.open() function. When I tested it out, it seems that major browsers like Chrome block the popup window. To me, the major function of open() is no longer useful anymore. So does this function still have any usage in current practice?

I am learning JS and stumbled across the window.open() function. When I tested it out, it seems that major browsers like Chrome block the popup window. To me, the major function of open() is no longer useful anymore. So does this function still have any usage in current practice?

Share Improve this question edited Dec 23, 2015 at 0:28 David Hoelzer 16.4k4 gold badges52 silver badges71 bronze badges asked Dec 23, 2015 at 0:04 Joshua LeungJoshua Leung 2,3998 gold badges33 silver badges56 bronze badges 2
  • 1 It's very useful, it's the only way to just open a blank window, but the user has to instanciate it by clicking something etc. otherwise it's what's called an "annoying popup", and most browsers try to block those. – adeneo Commented Dec 23, 2015 at 0:12
  • As @sg answered, chrome makes a lot of distinctions for actions if they're initiated by a user interaction event (see how it behaves with fullscreen requests). window.open is generally fine if you're using it properly, but <a target="_new"... may be preferable. – Dylan Watt Commented Dec 23, 2015 at 0:13
Add a ment  | 

2 Answers 2

Reset to default 12

I think Chrome only blocks window.open if it is not preceded by a user action. For example, if you have an element whose onclick attribute is mapped to a function...

function clickedButton() {
  window.open(...);
}

This would work. While this....

function clickedButton(){
  setTimeout(function(){
    window.open(...);
  })
}

would not.

So yes, it is still useful if you are able to set up your application in such a way that popups are only opened in response to a user action.

While it's true that generally opening new windows is a bad idea for reasons mentioned by Jonathan.Brink, I have used them before for authentication. Logging in through Facebook, for example, requires a new tab or a new window to be opened with their URL (iframes don't work). When it hits my website in its callback again, I close the window, and update the (responsive) website with new login information. Closing new tabs feels.... weird.

For some internal applications it may be useful, but the Mozilla docs remend against it:

Generally speaking, it is preferable to avoid resorting to window.open() for several reasons

Here are a few reasons why:

  • tab browsing is usually preferred to opening new windows
  • may not play nice with extensions/plugins
  • heavy on system resources
发布评论

评论列表(0)

  1. 暂无评论