I was used to do href="#"
but is there any other way?
Because clicking on such kind of a link sometimes can turn user to front of a page viewed and I want to load some scripts by clicking on a link.
I was used to do href="#"
but is there any other way?
Because clicking on such kind of a link sometimes can turn user to front of a page viewed and I want to load some scripts by clicking on a link.
Keep using <a href="#">Click</a>
, but use some Javascript/jQuery to prevent the page from jumping:
$('a').click(function() {
// do whatever
return false;
}
The return false line will prevent the browser from following the link, which in this case will stop it from jumping to the top of the page.
You should keep the link as an <a>
tag, rather than use a span
or div
, since this is far more semantic (i.e. users/crawlers will know it's supposed to do something since it's a link).
You should also avoid using inline Javascript (i.e. onclick="doSomething()"
) since this is a huge pain if you ever want to change the behaviour, and you also want to make your Javascript as unobtrusive as possible.
yes, href="#" makes ou scroll...
You can try:
<a href="javascript:void(0);" onclick="doIt(); return false;">Woho</a>
Why not use onclick
<a href="#" onclick="doMyThing() return false;" />
Similar discussion on SO, href-tag-for-javascript-links-or-javascriptvoid0
For a good markup
if you want an event on same page. So, it's better if you use div or span
for this & define your click event
on it.
div{
color:red;
}
JS
$('div').click{function(
)};
As @Yaniro commented you can use href="javascript:void(0)", the reason for this is:
JavaScript Void 0 Explanation
Web browsers will try and take whatever is used as a URL and load it. The only reason we can use a JavaScript Alert statement without loading a new page is because alert is a function that returns a null value. This means that when the browser attempts to load a new page it sees null and has nothing to load.
The important thing to notice here is that if you ever do use a JavaScript statement as the URL that returns a value, the browser will attempt to load a page. To prevent this unwanted action, you need to use the void function on such statement, which will always return null and never load a new page.
Extracted from: JavaScript Void 0 Explanation
You can simply use the onclick
attribute in any element, e.g.
<span onclick="foo()">Do something</span>
The habit of using href="#"
is a holdover from the early days when browsers did not support onclick
generally, only for links (in the technical sense: a
elements with href
attribute). That’s water under the bridge, but old habits often don’t die, especially if people just adopted them without knowing the reasons.
Instead of span
, you could use a
too. Without any href
attribute, an a
element has no special meaning.
By nondisruptiveness principles, the element should be generated dynamically with JavaScript instead of being a static part of the page (since when JavaScript is disabled, it would just sit there, making an impression of being relevant, but without doing anything).