Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use window.open method in an <a> tag? 1

Status
Not open for further replies.

royboy75

Programmer
Feb 13, 2007
114
GB
Hello,

I have a standard link in my html <a href="....
I would like to override it's normal behaviour and open a window that I decide it's size, toolbar ect.
For that, I have added it an onClick event and wrote window.open in the appropriate javascript function.
The problem is that the target at href is opened with this window and if I try to put an empty href <a href=""... it doesn't work at all.
Any ideas how to accomplish this?
 
You need to return false.

Code:
<a href="[URL unfurl="true"]http://www.google.com"[/URL] onclick="popUp(); return false;">Sample</a>

You can do it from your function...

Code:
<script type="text/javascript">
function popUp() {
   // something
   return false;
}
</script>

<a href="[URL unfurl="true"]http://www.google.com"[/URL] onclick="return(popUp());">Sample</a>

Cheers,
Jeff


[tt]Jeff's Page [!]@[/!] Code Couch
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Thanks Jeff that indeed worked, I get the url at the window.open and not the one at the href part.
Can you please explain me though why the reurn false cancels the navigation at the href part?
 
It's effectively cancelling the bubbling of the click event. Once the onclick fires and returns false, the event never makes it back up the "chain" to the <a> node... effectively cancelling the navigation to another URL.

That's how I understand it to work at least [smile]

Cheers,
Jeff


[tt]Jeff's Page [!]@[/!] Code Couch
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top