简体   繁体   中英

Original page redirects while new window is blank when using Window.Open

I've created a function and am calling it but it doesn't seem to work. A new window opens, but it ends up blank and the original page is the one that is redirected to where the new window is supposed to.

Here is my script:

 var bikeWindow;
         function showBike(linkTarget) {
             bikeWindow = window.open(linkTarget, "bikeInfo", "toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,width=620,height=575");
             bikeWindow.focus();
         }

Here is my function call:

a href="cannondale.html"  onclick="return showBike();"

Any tips? Thank you.

Your problem is that your function expects a targetLink variable as parameter to be used as redirection url, you need to pass the link href to your function, using this.href .

This is how should be your code:

JavaScript:

var bikeWindow;

function showBike(event, linkTarget) {
  event.preventDefault();
  bikeWindow = window.open(linkTarget, "bikeInfo", "toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,width=620,height=575");
  bikeWindow.focus();
}

HTML:

<a href="cannondale.html" onclick="showBike(event, this.href);">Link</a>

Note:

The return keyword before your function call is useless in the link click event.

Edit:

By using this.href on the link we are passing the href attribute value to our function: where this refers to the <a> link itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM