Open new tab in Firefox and Edge with JS

Asked

Viewed 109 times

2

Due to the restriction that the browser makes the scripts internal rounds, asking for confirmation using the code:

window.open()

found the following way to remove this permission from the user:

var win = window.open();
win.location = URL;
win.opener = null;
win.blur();
win.focus();

works in Chrome, but in Firefox and Edge no, gives this error:

Unable to set Property 'Location' of Undefined or null Reference

Does anyone know a way to get this action across all browsers?

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

1 answer

5

It depends on what you want to do. If you look at documentation of the method open() of the object window you will see that it has 3 parameters, and 2 of them are mandatory, and you are not passing them, so it cannot produce a suitable result and it generates a null value, after which you obviously cannot make any operation work on an object that even exists. Opening a window is an operation that can fail a lot, in my browser will always fail, can not trust it.

One thing I see a lot, especially in JS codes is that people use objects without being sure they’re there. Even if you do everything right you have several reasons to go wrong and any operation should be tested before you try to run it. This technique adopted by all JS is error code (he returns a null if something went wrong) and how it was abused and given in the hand of inexperienced programmers in general gives a lot of trouble, so it is important to change the way you do things and not trust the results you get ever, even when it seems you did everything right. For this it is necessary to read in detail the documentation of everything you will use, otherwise you will have a lot of unexpected errors in what you are doing and have no idea why it occurs, what to do, or even play it because it may occur only in certain scenarios.

In this specific case it is not even a one-off error, it is a programming error and although it is still valid and necessary to check if the object was created properly, for your code before that you need to call the method correctly passing at least a URL and a window name, as instructed by the documentation. But it does not mean that this is solved, remember that a lot can fail, just the fact that you think you can always open a window is an alert that connects in your code. If you have done something that depends on the window opening to work has already done something wrong, this is not available in all cases.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.