I was searching for a way to prevent being taken to the top of a page after clicking a link with #
inside the href
attribute:
<a href="#" onclick="foobar()">click me</a>
and then I came across this simple solution, switching #
for #!
:
<a href="#!" onclick="foobar()">click me</a>
There were various other solutions available online using jQuery for instance:
$('.service').click(function(e) {
e.preventDefault();
});
I ended up liking #!
the most for being so clean BUT I COULDN'T find any documentation anywhere online as to what was actually happening when inserting the !
in the line of code.
QUESTION:
What is #!
actually causing the html code to do? Is this a good way to prevent the default action of taking the user to the top of the page, or is this method bad for other reasons? What I'm afraid of is that it might be a hacky method that isn't compatible with some browsers.