Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add target="_blank" to JavaScript window.location?

Tags:

javascript

People also ask

Can you add target _blank to URL?

You can use the target="_blank" attribute if you want your users to click on a link that opens up a new browser tab. The target="_blank" attribute is used inside the opening anchor tag like this.

What is target _blank in Javascript?

The HTML target attribute defines where the linked document will open when the user clicked on the link. If target=”_blank” is set with anchor element, then the linked document will open in a new tab otherwise document is open in the same tab.

Is Target _blank deprecated?

It looks like target="_blank" is still alright. It is listed as a browsing context keyword in the latest HTML5 draft.

Can you add target _blank in CSS?

Unfortunately, no. In 2013, there is no way to do it with pure CSS.


window.location sets the URL of your current window. To open a new window, you need to use window.open. This should work:

function ToKey(){
    var key = document.tokey.key.value.toLowerCase();
    if (key == "smk") {
        window.open('http://www.smkproduction.eu5.org', '_blank');
    } else {
        alert("Kodi nuk është valid!");
    }
}

Just use in your if (key=="smk")

if (key=="smk") { window.open('http://www.smkproduction.eu5.org','_blank'); }

I have created a function that allows me to obtain this feature:

function redirect_blank(url) {
  var a = document.createElement('a');
  a.target="_blank";
  a.href=url;
  a.click();
}

if you want to open a link in new tab window.open has a problem, my firefox detect a popup link by using window.open' and this is not good

I was handle this by using the latest comment

var anchor = document.createElement('a');
anchor.href = 'https://example.com';
anchor.target="_blank";
anchor.click();