Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent redirect after hyperlink click?

I have following html+ js code:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>

<a href="#" id=key onclick="func(0)">foo</a>

</body>

<script type="text/javascript">
function func(k){
    alert(1);
    return false;
}   
</script>
</html>

Can you explain how to refactor following code that after click on href code func executes but # doesn't add to the URL and page shouldn't be reload?

like image 855
gstackoverflow Avatar asked Oct 20 '14 20:10

gstackoverflow


People also ask

How do I stop a link from redirecting?

From the drop-down menu select Settings then scroll down and click Advanced. In the Privacy & security section choose Content settings > Pop-ups and redirects then ensure that the Allowed option is turned off.

How do I redirect a specific part of a URL?

One can use the anchor tag to redirect to a particular section on the same page. You need to add ” id attribute” to the section you want to show and use the same id in href attribute with “#” in the anchor tag.

How do I automatically redirect a page after 5 seconds?

To redirect a webpage after 5 seconds, use the setInterval() method to set the time interval. Add the webpage in window. location. href object.


1 Answers

Use javascript:void(0); instead of # as follows:

<a href="javascript:void(0);" id=key onclick="func(0)">foo</a>
like image 120
benomatis Avatar answered Oct 04 '22 20:10

benomatis