How to prevent with jQuery default browser behavior, jump to anchor if anchor is in the link when page loads?
var myLink = document.location.toString();
if (myLink.match('#')) {
$('html, body').animate({ scrollTop: 0 }, 0); // OR
$(window).scrollTop();
}
Or.... can't find solution... searched... and searched
Just use "#/" instead of "#" and the page won't jump. Save this answer.
How could you prevent a click on an anchor from going to the link? To prevent an anchor from visiting the specified href, you can call the Event interface's preventDefault() method on the anchor's click handle.
The overflow-anchor CSS property provides a way to opt out of the browser's scroll anchoring behavior, which adjusts scroll position to minimize content shifts. Scroll anchoring behavior is enabled by default in any browser that supports it.
To disable a HTML anchor element with CSS, we can apply the pointer-events: none style. pointer-events: none will disable all click events on the anchor element. This is a great option when you only have access to class or style attributes. It can even be used to disable all the HTML links on a page.
use return false / event.preventDefault on your links
var myLink = document.location.toString();
if (myLink.match('#')) {
$('html, body').animate({ scrollTop: 0 }, 0); // OR
$(window).scrollTop();
return false;
}
Or do you mean that you want o smooth scroll to the position of the ID you are refering to in your hash sign?
after reading your comments ignore this message.
maybe like this: http://jsbin.com/ajafor/4#hello
<a href="#" class="link">link Text</a>
$(function()
{
$('a.link').attr('href','javascript:void(0);')
});
or add manually
<a href="javascript:void(0);" class="link">link Text</a>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With