Alex Ritter
Alex Ritter

Reputation: 1041

window.open opens link in new window and on original page

I am trying to create a link that opens a remote website page in a pop-up. I did a bit of googling, and came up with the following code:

<a href="http://www.yandasmusic.com" onclick="window.open(this.href,'targetWindow','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=400,height=400')" >A pop-up link</a>

This code does open up the specified page in a new window, but for some reason it also loads the page on the original window. How can I change this code so that the pop-up opens the specified page, but does not change the page of the original window?

Thanks - Alex

Upvotes: 1

Views: 1668

Answers (2)

user2092317
user2092317

Reputation: 3338

Just add return false , Because the default action is true always.

The return value of an event handler determines whether or not the default browser behavior should take place as well. In the case of clicking on links, this would be following the link, but the difference is most noticeable in form submit handlers, where you can cancel a form submission if the user has made a mistake entering the information.

change

<a href="http://www.yandasmusic.com" onclick="window.open(this.href,'targetWindow','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=400,height=400')" >A pop-up link</a>

with

<a href="http://www.yandasmusic.com" onclick="window.open(this.href,'targetWindow','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=400,height=400'); return false;">A pop-up link</a>

Upvotes: 0

kavun
kavun

Reputation: 3381

Add return false; at the end of the onclick

<a href="http://www.yandasmusic.com" onclick="window.open(this.href,'targetWindow','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=400,height=400'); return false;">A pop-up link</a>

Upvotes: 1

Related Questions