How to open a link in a new tab by clicking a button? to jade

Following

button(type="button", target="_blank", onclick="location.href='auth/google';") 

does not work. It opens the link in the same window. Just for reference, its part of the node.js program in which I use passjs for Google authentication.

+6
source share
1 answer

The button doesn’t actually open the link - it just runs some kind of javascript code, which, in this case, should go to the new URL. Thus, the target="_blank" attribute on the button will not help.

Instead, you need to use javascript commands to open a new tab / window, and not use javascript to change the url of the current window. Assigning location.href will only change the URL of the current window.

Instead, use the window.open(url, target) function β€” this requires a URL and the name of the target window, which behaves the same as the target="whatever" attribute on the link.

 window.open('auth/google', '_blank'); 

Your complete code would look like this:

 button(type="button", onclick="window.open('auth/google', '_blank');") 
+19
source

Source: https://habr.com/ru/post/950452/


All Articles