Search code examples
javascriptbrowserwindow.open

How to open a new tab in Javascript?


I would like to open in a new tab a page of my website.

I've tried to use the window.open function but it seems like it's not supported anymore.

Those are a few options I've tried in local :

url = "localhost:3000/my-other-page"
window.open(url, '_blank');

OR

var popup  = window.open("about:blank", "_blank");
popup.location = url;

The first option opened the page in a new tab but the screen stayed black.

What would be the best way to open this url ? Is it still supported by current browsers?


Solution

  • window.open is supported and works fine… providing you use it in response to a user triggered event.

    For example, you are allowed to open a new window when the user clicks on a button, but you aren't allowed to open one as soon as the page loads.

    This is an anti-spam / resource bombing measure.


    re edit

    url = "localhost:3000/my-other-page"
    

    Your URL starts with the hostname, but is missing the markers that stop it being treated as a path.

    Use the correct URL:

     url = "http://localhost:3000/my-other-page"