Checking if new tab or window can be opened in javascript

1.3k views Asked by At

Is there a way to check if the current browser supports multiple tabs/windows. This is a common thing now because of in-app browsers.

1

There are 1 answers

3
visibleman On

According to mdn window.open docs, the window.open() call should return null if the call was unsuccessful.

Return value

A WindowProxy object, which is basically a thin wrapper for the Window object representing the newly created window, and has all its features available. If the window couldn't be opened, the returned value is instead null. The returned reference can be used to access properties and methods of the new window as long as it complies with Same-origin policy security requirements.

As far as checking the ability to open a window without actually trying to open a window, there is no available API for that (as far as I know). You could open and close the window immediately again. Depending on the browser, the user may or may not see the brief flashing of a new window or tab.

function window_open_check()
{
  w=window.open()
  if (w !== null){
    w.close()
    return true
  }
  return false
}

// Note:window.open() is blocked in StackOverflow scripts
console.log("window.open available:",window_open_check());