-3

If the user clicks a link, I want to call a Javascript function if JS is enabled, or go to a URL if it's disabled. How to solve this? I've tried

<a href="/url?img=2" onclick="changeImage(2); return false;">

but it doesn't seem to work.

Andreas
  • 7,470
  • 10
  • 51
  • 73
  • 4
    It should work. When JavaScript is enabled, and `changeImage` does not throw any errors, the link will not be followed. Otherwise, `/url?img=2` is loaded. – Rob W Jan 15 '12 at 14:10
  • 2
    In what circumstances does it not work? – Marcin Jan 15 '12 at 14:10
  • 2
    This should work. Please describe what does "doesn't seem to work" mean. The only explanation is that there's error in `changeImage`, check JavascriptError console. – Vyktor Jan 15 '12 at 14:12

1 Answers1

1

Try returning false from your JavaScript function. This causes the browser to halt and not follow the link after your function is finished.

I'm assuming that when you click the link now, you are just taken to the url and the JavaScript does not seem to work. Is that correct?

Matthew
  • 8,183
  • 10
  • 37
  • 65