Can I find when the window.onclick () function is overridden?

in javascript, is it possible to make an event handler when a function attribute changes from

window.onclick = function() { ....function 1...... }; 

to

 window.onclick = function() { ....function 2..... }; 
+5
source share
2 answers

It's been a few years, but he did a great job for me. An SO user published an open source surveillance object several years ago. It changes the prototype of the object so that you track changes in certain properties of the object.

 window.watch('onclick', function() { console.log('changed'); }); window.onclick = function() { console.log('click1'); }; window.onclick = function() { console.log('click2'); }; 

JSFiddle: https://jsfiddle.net/gou48xpa/

Source code: https://gist.github.com/eligrey/384583 (released under the MIT license)

 if (!Object.prototype.watch) { Object.defineProperty(Object.prototype, "watch", { enumerable: false , configurable: true , writable: false , value: function (prop, handler) { var oldval = this[prop] , newval = oldval , getter = function () { return newval; } , setter = function (val) { oldval = newval; return newval = handler.call(this, prop, oldval, val); } ; if (delete this[prop]) { // can't watch constants Object.defineProperty(this, prop, { get: getter , set: setter , enumerable: true , configurable: true }); } } }); } // object.unwatch if (!Object.prototype.unwatch) { Object.defineProperty(Object.prototype, "unwatch", { enumerable: false , configurable: true , writable: false , value: function (prop) { var val = this[prop]; delete this[prop]; // remove accessors this[prop] = val; } }); } 
+7
source

use the callback container for your custom function using addEventlistener or attachEvent

 element.attachEvent('onclick', handler); 

or

 element.addEventlistener('onclick', handler, false) 

you can transfer it to some function

0
source

All Articles