html - JavaScript get href onclick -


i trying return href attribute of link using javascript when user clicks on it. want url link linking displayed in alert instead of loading page.

i have following code far:

function doalert(){     alert(document.getelementbyid("link").getattribute("href"));     return false; } 

with following markup:

<a href="http://www.example.com/" id="link" onclick="return doalert()">link</a> 

for reason, no alert ever displayed , page loads instead. know why is?

using jquery not option in case.

seems order of code, try this

<script>     function doalert(obj) {         alert(obj.getattribute("href"));         return false;     } </script> <a href="http://www.example.com/" id="link" onclick="doalert(this); return false;">link</a> 

http://jsfiddle.net/yz8yv/

http://jsfiddle.net/yz8yv/2/


Comments

Popular posts from this blog

wireshark - USB mapping with python -

c++ - nodejs socket.io closes connection before upgrading to websocket -

Deploying Qt Application on Android is really slow? -