Last active
August 29, 2015 14:05
-
-
Save jneurock/211708cb96cbea1d7cd3 to your computer and use it in GitHub Desktop.
Add a URL property to the original event object jQuery wraps
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
(function($) { | |
// Capture the original $.Event constructor | |
$._Event = $.Event; | |
// Override the $.Event constructor (preserve the 2 arguments) | |
$.Event = function(src, props) { | |
// If the src argument is truthy, add a URL property to it | |
if (src) { | |
src.URL = window.location.href; | |
} | |
// Call the original $.Event constructor | |
$._Event.call(this, src, props); | |
}; | |
// Make sure the new $.Event prototype matches the old one | |
$.Event.prototype = $._Event.prototype; | |
})(jQuery); | |
// Later on... | |
$('selector').on('event', function(evt) { | |
if (evt.originalEvent) { | |
// Do something with evt.originalEvent.URL | |
console.log(evt.originalEvent.URL); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment