Click here. ...">

Does javascript onclick support cross browser?

Can I use the following in all browsers?

<a href="#" onclick="doSomething()">Click here.</a>

Is this “bad practice” regarding standards?

Will it work with IE, FF, Safari and Chrome?

+5
source share
6 answers

Can I use the following in all browsers?

Yes

Is this “bad practice” regarding standards?

“Bad practice” and “compliance with standards” are two different things. This is up to standards, but also for three reasons for bad practice.

  • It is not unobtrusive . Event handlers are better used with JS.
  • It links to the top of the page ( #) and will always send the browser there, even if JS starts up.
  • JS , . , .

IE, FF, Safari Chrome?

+9

, , ( , , ), ( ) - , .

, Javascript , ( # href , , , ). , Javascript, - , href .

- :

<a href="[url to JS-less way of doing the same thing]" onclick="doSomething(); return false">Click here</a>

, javascript-only, CSS Javascript, ( JS ).

+6

onclick - -. , . , " " . , - , javascript . , , , , ..

+2

, .

; Javascript:

<a href="#" onclick="doSomething();">Click here.</a>

, , , , .

, # :

<a href="#" onclick="doSomething(); return false;">Click here.</a>
0

onClick Common/W3C , .

, , - .

0

, , , , jQuery $('a').click(doSomething);, JavaScript HTML. jQuery live HTML, .

0

All Articles