2

I have a standard link setup that fires an event via jquery when clicked

<a href="#" class="dosomething">Click Me</a> 

All that works great, except that when the pseudo URL is clicked, it appends a hashtag (#) to the url. This hashtag affects how my page reloads if the user decides to refresh the page later on, so i'd like to not have the hashtag appended to the url.

is this possible while still allowing my normal jquery to fire?

Thanks!

Faisal Sayed
  • 783
  • 4
  • 12
Mark
  • 3,653
  • 10
  • 30
  • 62

4 Answers4

4

You should either return false; from the event handler of A tag

Or, use

<a href="javascript: void(0);" class="dosomething">Click Me</a> 

For those who thinks javascript: void(0) is bad practice

If you use href='#', you must take care of two things

// one
function fn() {
    // code
    return false;
}

// two
<a href="#" onclick="return fn();">click</a>

And if you forget and just write onclick="fn();" it won't work

Another thing why I used javascript: void(0); is, if the function encounters/throws an error, it wont return false

So if you're a lone developer then you can clearly make your own choice, but if you work as a team you have to either state:

Use href="#", make sure onclick always contains return false; at the end, that any called function does not throw an error and if you attach a function dynamically to the onclick property make sure that as well as not throwing an error it returns false.

OR

Use href="javascript:void(0)"

Which "href" value should I use for JavaScript links, "#" or "javascript:void(0)"?

Community
  • 1
  • 1
Salman
  • 9,299
  • 6
  • 40
  • 73
  • `javascript: void(0)` is bad pratice. on hrefs (and most the time i think) – VeXii Feb 20 '13 at 17:42
  • @VeXii may be the updated answer will explain you why I used `javascript: void(0)` – Salman Feb 20 '13 at 17:53
  • @VeXii Absolutely agreed on that point. but the question was not to improve the code, so I gave the answer to correct the problem only. – Salman Feb 20 '13 at 17:59
  • What do you mean? din't get you – Salman Feb 20 '13 at 18:15
  • Man the OP is not even concerned about what is the best practice. He already selected what he wanted to do. You are free to make the changes if you want. btw, you already mentioned that in your answer. – Salman Feb 20 '13 at 18:19
  • yes and its mentioned in youre link and sevral other places but way to often do ppl ask for a solution and get somthing like "use # or void(0)" even thou the ppl saying it know its wrong – VeXii Feb 20 '13 at 18:30
0

In end of the you click function, use:

return false;
FabianoLothor
  • 2,752
  • 4
  • 25
  • 39
0

smartass anwser: use a button.

alternative: you must make sure to trigger the preventDefault in youre jQuery event handler

$("dosomthing").click(function(e){
  //make magic happen
  e.preventDefault()
})

this works on html forms thats submitting and such.

note on the button thing

it is best pratice to only use a tags for link (somthing that changes the url) and buttons for other sorts of interactions.

search bots and other web crawlers expect a tags to link to a other html document (hyperlink) and up to and including html 4. or to a other point in the current document.

VeXii
  • 3,079
  • 1
  • 19
  • 25
-4

Does it need to be an href at all? you could do:

<span class="dosomething">Click me</span>

.

.dosomething{cursor:pointer}
Fraser
  • 14,036
  • 22
  • 73
  • 118