How to execute onClick and href like in Anchot tag (<a>)
4 answers
I think the easiest way to do this is to return the function true, so that after the function completes, the anchor tag will behave as usual. Thus, you can also use the target attribute if you want to use a new window:
<a href='http://www.google.com' onclick="myFunction();" target="google">Click me</a>
Script
<script type='text/javascript'>
function myFunction() {
alert("hello");
return true;
}
</script>
+7
I used jQuery, I hope it is still clear:
<a href="www.google.com" onClick=someFunction()>Click me </a>
Executes the js function, but does not lead me to the href link.
So, ONE way to add redirection after your function:
function someFunction()
{
... your code ...
window.location.href( $(this).attr('href') ); // <----- HERE
}
:
function someFunction()
{
... your code ...
window.open( $(this).attr('href') , '_blank' ); // <----- HERE
}
+3
, .
.click 'a', " " javascript.
, "click" javascript. "a" onClick, ( , ).
, , "click" 'a', href onClick, .
onclick, href.
:
$('a').click(function () {
window.open($(this).attr('href'));
});
http://jsfiddle.net/4Qku8/, jquery.
-2