Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is window.open() still useful nowadays?

Tags:

javascript

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?

like image 213
Joshua Leung Avatar asked Dec 23 '15 00:12

Joshua Leung


People also ask

Is window open a popup?

The syntax to open a popup is: window. open(url, name, params) : url. An URL to load into the new window.

What is the second window open () method?

The open() method opens a new browser window, or a new tab, depending on your browser settings and the parameter values.

Is window open synchronous?

Using Chrome's window. Native window. open() allows synchronous access to opened windows so it is convenient choice if you need to open a dialog or a preferences window.

Is Windows an open system?

Because of this interoperability and portability, and despite Windows being a proprietary OS, a computer running the Windows OS can be considered as an open system.


1 Answers

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.

like image 139
sg.cc Avatar answered Nov 07 '22 21:11

sg.cc