21

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?

2 Answers 2

31

Show your sample url, I think you have to use a full protocol url, instead of a domain name.

ex:

Use https://google.com instead of google.com

var url =  'https://google.com';
window.open(url, '_blank');
Sign up to request clarification or add additional context in comments.

4 Comments

I see, could it be because I've tried with localhost:3000 urls ? Do you think that it would work with production urls ?
@Benjamin I don't think so :-? Are you setting open a new tab automatic?
The new tab should open when the user click on a link. But I have to do that in javascript .. Which should be pretty standard I guess
Do you try url = "http://localhost:3000/my-other-page" instead of url = "localhost:3000/my-other-page"?
14

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"

1 Comment

Well.. As simple as it gets .. It works, thank for your time and your explanation !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.