javascript - Why is this 'href' value not working in Opera? -
i have following:
<a href="javascript:jquery('body').css('backgroundcolor','red');">test</a>
when run in chrome, functions expected , turns page red. however, in opera get:
[object object]
closer inspection reveals opera thinks javascript:query('body')...
kind of url. doing wrong? doesn't opera recognize javascript:
links in href
attribute?
jsfiddle: http://jsfiddle.net/9czzl/
edit: seems firefox problem too...
the issue return value of jquery('body').css('backgroundcolor','red')
object, browsers interpret new content web page. fix this, can use javascript void
operator turn undefined
, ff , opera (and potentially others) handle intended. notice issue described on page, since it's premier use-case of void
operator (other code golf void 0
less characters undefined
).
<a href="javascript:void(jquery('body').css('backgroundcolor','red'));">test</a>
this should give intended result: http://jsfiddle.net/9czzl/13/
it should noted handling clicks way considered bad practice. instead, using event handlers in javascript recommended. helps separate different layers of web app, in turn make debugging in future easier.
Comments
Post a Comment