Matt Winer
Matt Winer

Reputation: 535

OnClick opens new windows but also changes current window

I'm using IE8. I've setup the following link:

<a href="terms.html" onclick="window.open(this.href,'targetWindow','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=400,height=600')" />Terms & Conditions</a>

When I click it. I get the pop up the way I want. But it also sets the current windows to the same URL. I know I'm missing something silly. Just can figure it out.

Thanks!

Upvotes: 2

Views: 1895

Answers (2)

user3041160
user3041160

Reputation: 684

Add return false; after window.open like below :

<a href="terms.html" onclick="window.open(this.href, 'targetWindow', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=400,height=600');return false;" />Terms & Conditions</a>

Upvotes: 3

bvx89
bvx89

Reputation: 888

Since you're clicking a <a>-link, your browser will automatically follow it. You need to make the onclick method return false for it to not open that page in your current window. It's also good to extract all JavaScript code from HTML, so that's something you should look into. However, this solution seems to do the trick:

HTML

<a href="terms.html" onclick="return openWindow('terms.html')" />Terms & Conditions</a>

JavaScript

function openWindow(href) {
  window.open(href,'targetWindow','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=400,height=600');
  return false;
}

Upvotes: 1

Related Questions