I'm dealing with a situation where I need to bind jQuery events to a page to handle UI updates that is being generated via JSF. Alas, JSF sucks and it sticks onclick events on everything, which pre-empt any of my jQuery work.
Thanks to the fine folks here on SO, I found a solution for that:
mixing my jQuery click events with existing object's onclick attribute
The logic is:
- on page load, grab all of the
onclick
attributes and store them in a variable. - bind my jQuery events
- after my own jQuery events, I can then
eval
the original onclick:eval(onclickValueVariable)
This worked find in all my dummy onclick event testing.
But then it failed on that live JSF page. The issue is that all of the onclick's end with a 'return false' which leads to this error:
return not in function
For example:
<div class="uglyJSFcreatedTag" onclick="console.log('hey');return false">
And the jQuery that would fire it:
var $jsfTag = $('.uglyJSFcreatedTag');
var cacheOnclick = $jsfTag.attr(onclick);
$jsfTag.removeAttr('onclick');
...bunch of logic here to decide if the inline onclick should fire and, if so...
eval(cacheOnclick)
The problem is the return false
. It looks like nothing can be returned when firing a eval.
Is there a solution for this? I imagine I could take the value as a string and do string parsing to remove all return false, then call that from the script itself. But that sounds a bit messy. Is there a more elegant solution?
If not, what particular JS statements should I be looking for to filter out before calling eval
?