Created
September 13, 2010 01:04
-
-
Save dparoulek/576669 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).ready(updateLinks); | |
// Remove all event handlers from elements that have the 'elementMatcher' somewhere in the event handler function | |
function updateEventHandlers(elementMatcher, eventHandlerType, urlFrom, urlTo){ | |
jQuery.each(jQuery(elementMatcher), function(){ | |
var methodString = jQuery(this).attr(eventHandlerType).toString(); | |
//alert("Going to remove, update and then add the following method back:\n"+ methodString) | |
//create an "oldmethod" attribute and assign it the FUNCTION DEFINITION of the even handler method (in order to cache it for later use) | |
jQuery(this).attr("oldmethod", jQuery(this).attr(eventHandlerType).toString()); | |
//remove the function bind | |
jQuery(this).removeAttr(eventHandlerType); | |
}); | |
//replace click events | |
jQuery("input").live("click", function(e){ | |
//grab the "cached" function definition for the originally bound onclick method | |
var methodString = jQuery(this).attr("oldmethod"); | |
if(methodString && methodString.indexOf("onclick") != -1 && methodString.indexOf(urlFrom) != -1){ | |
//remove the " onclick" leaving just an annonymous function definition | |
var reg = / onclick/; | |
methodString = methodString.replace(reg, ''); | |
//replace the "urlFrom" | |
methodString = "function(){alert('hello world')}" //methodString.replace(urlFrom, urlTo); | |
//alert("New method:\n" + methodString); | |
//create a function named clickMethod holding the anonymous function created above | |
eval("clickMethod = " + methodString); | |
//execute the clickMethod function (call the original click event) | |
clickMethod(e); | |
return false; | |
} | |
}); | |
//replace blur events | |
jQuery("input").live("blur", function(e){ | |
//grab the "cached" function definition for the originally bound onclick method | |
var methodString = jQuery(this).attr("oldmethod"); | |
if(methodString && methodString.indexOf("onblur") != -1 && methodString.indexOf(urlFrom) != -1){ | |
//remove the " onclick" leaving just an annonymous function definition | |
var reg = / onblur/; | |
methodString = methodString.replace(reg, ''); | |
//replace the "urlFrom" | |
methodString = "function(){alert('hello world')}" //methodString.replace(urlFrom, urlTo); | |
//alert("New method:\n" + methodString); | |
//create a function named blurMethod holding the anonymous function created above | |
eval("blurMethod = " + methodString); | |
//execute the clickMethod function (call the original click event) | |
blurMethod(e); | |
return false; | |
} | |
}); | |
} | |
function updateLinks(){ | |
// update onclick and onblur event handlers that contain the string "/some-url" | |
updateEventHandlers("input[onclick*=/some-url]", "onclick") | |
updateEventHandlers("input[onblur*=/some-url]", "onblur") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment