Flamur Beqiraj Flamur Beqiraj - 1 year ago 207
Javascript Question

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

The following sets the target to "_blank":

if (key=="smk") { window.location="http://www.smkproduction.eu5.org"; target="_blank"; done=1; }

But this doesn't seem to work. How do I launch the link in a new tab?

Here is my code:

<script LANGUAGE="JavaScript">
<!-- Begin
function ToKey(){
var done=0;
var key=document.tokey.key.value;
if (key=="smk") { window.location="http://www.smkproduction.eu5.org"; target="_blank" done=1; }
if (done==0) { alert("Kodi nuk është valid!"); }
// End -->

<!-- STEP TWO: Paste this code into the BODY of your HTML document -->


<form name="tokey">
Type the key
<input type="text" name="key">
<input type="button" value="Go" onClick="ToKey()">

Answer Source

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") {
    } else {
        alert("Kodi nuk është valid!");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download