I have a div that pops up when a user clicks on a link that will take them off of the english version of the site. They are given a yes or no choice to proceed. I have to capture the "URL" of where they will开发者_高级运维 go to if they click yes. This is easy on the links that are a hrefs, but some of the links are onClicks that execute JavaScript. I have to capture the JavaScript, keep it from executing, and then execute it if they click yes. This is the capture onClick code I've got, but how do I get it to work?
$_('a#foo').live('click', function(event) {
var onclick = $(this).attr("onclick");
$_('#yes a').attr('onclick', onclick);*/
return false;
event.preventDefault();
});
the yes button/div
$_('#yes').live('click', function() {
if(href) {
window.location=href;
};
if(onclick) {
eval(onclick);
}
});
Here's a working demo
Basically, if an onclick attribute is defined in the html, it'll be accessible as somelink.onclick
which is just a function, so there's no need for eval
The trick is however, that code defined in an onclick-attribute will execute before the jQuery live()
event handler, so you have to avoid that by removing the original onclick function but keeping a copy you can execute later.
精彩评论