0

How can I open a URL specified in window.location.href in new window?

function callThisFunction1() {
    var dropdown = document.getElementById("lookup1");
    var unit = dropdown.options[dropdown.selectedIndex].value;
    if (unit == "M") {
        window.location.href='site.com'+document.getElementById("Var").value;
    }
    else if (unit == "S") {
        window.location.href='site.com'+document.getElementById("Var1").value;
    }
}
Stephen P
  • 14,422
  • 2
  • 43
  • 67
Matt
  • 45
  • 1
  • 9
  • 2
    https://developer.mozilla.org/en-US/docs/Web/API/Window/open – Teemu Aug 26 '19 at 18:23
  • I think you'll need to use the window.open() function instead of `window.location.href=` There's some details about that here: https://stackoverflow.com/questions/726761/javascript-open-in-a-new-window-not-tab?rq=1 https://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-and-not-a-new-window-using-javascript?noredirect=1&lq=1 – Michael S Aug 26 '19 at 18:25
  • 1
    To add: `window.open()` behaves differently with absolute and relative URLs. If you want to go to a different site all together make sure to add `http://` or `https://`. Example: `window.open("https://www.youtube.com", "_blank")`. – ambianBeing Aug 26 '19 at 18:39
  • Possible duplicate of [JavaScript open in a new window, not tab](https://stackoverflow.com/questions/726761/javascript-open-in-a-new-window-not-tab) – Stephen P Aug 26 '19 at 18:40

4 Answers4

2

You can use window.open(url) instead of location.href

window.open() is a method that you can pass a URL to that you want to open in a new window while window.href is not a method, it's a property that will tell you the current URL location of the browser. Changing the value of the property will redirect the page.

Example:

window.location.href = url; //Will take you to URL.

window.open(url); //This will open url in a new window.

window.open() can be used with parameters

DimoMohit
  • 735
  • 4
  • 17
2

This should do the trick. Here i am assuming that you want to current location.href in new tab. If you want some other url, you can open that as well. Just pass the value in window.open.

<!DOCTYPE html>
<html>
<body>

<p>Should open href in new window</p>

<button onclick="openTab()">Click here</button>

<script>
function openTab() {
  window.open(window.location.href);
}
</script>

</body>
</html>
Mahendra Pratap
  • 331
  • 1
  • 6
1

function onDropownChange(e){

  var value = document.getElementById('dropDown').value;
  switch(value){
    case 'A':
      window.open('https://www.google.com','_blank');
    case 'B':
      window.open('https://stackoverflow.com/','_blank');
  }

}
<!DOCTYPE html>
<html>
<head>
 <title>Example</title>
</head>
<body>

<select name="example" id="dropDown" onchange="onDropownChange()">
 <option value="A">Option A</option>
 <option value="B">Option B</option>
</select>

<script>

</script>

</body>
</html>

Please replace this window.location.href='site.com'+document.getElementById("Var1").value; and this window.location.href='site.com'+document.getElementById("Var").value; line as followos

window.open('site.com'+document.getElementById("Var1").value,'_blank');

I hope this will help.

0

You are telling window to change the current frame location by calling:

window.location.href = 'mysite.com';.

What you want to do is to issue a new frame, you'll have to use window.open() method instead:

var newWindow = window.open([url],[newWindowName],[config]);

full reference here: https://developer.mozilla.org/es/docs/Web/API/Window/open

Leonardo Moreno
  • 162
  • 2
  • 8