javascript - Open window.location.href location in a new window - Stack Overflow

How can I open a URL specified in window.location.href in new window?function callThisFunction1() {var

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'+document.getElementById("Var").value;
    }
    else if (unit == "S") {
        window.location.href='site'+document.getElementById("Var1").value;
    }
}

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.'+document.getElementById("Var").value;
    }
    else if (unit == "S") {
        window.location.href='site.'+document.getElementById("Var1").value;
    }
}
Share Improve this question edited Aug 26, 2019 at 18:36 Stephen P 14.8k2 gold badges48 silver badges70 bronze badges asked Aug 26, 2019 at 18:20 MattMatt 651 silver badge9 bronze badges 4
  • 2 developer.mozilla/en-US/docs/Web/API/Window/open – Teemu Commented Aug 26, 2019 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: stackoverflow./questions/726761/… stackoverflow./questions/4907843/… – Michael S Commented Aug 26, 2019 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.", "_blank"). – ambianBeing Commented Aug 26, 2019 at 18:39
  • Possible duplicate of JavaScript open in a new window, not tab – Stephen P Commented Aug 26, 2019 at 18:40
Add a ment  | 

4 Answers 4

Reset to default 2

function onDropownChange(e){

  var value = document.getElementById('dropDown').value;
  switch(value){
    case 'A':
      window.open('https://www.google.','_blank');
    case 'B':
      window.open('https://stackoverflow./','_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.'+document.getElementById("Var1").value; and this window.location.href='site.'+document.getElementById("Var").value; line as followos

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

I hope this will help.

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>

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

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

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

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/es/docs/Web/API/Window/open

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742357701a4428769.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信