Home > Software engineering >  How to create listener on document.createElement?
How to create listener on document.createElement?

Time:10-09

How to create listener on document.createElement func?

For example, after

var canvas = document.createElement("canvas");

it was done automatically

console.log("createElement worked")

P.S. how to do the same but with different functions, for example toDataURL

var str = canvas.toDataURL("image/png");

it was done automatically

console.log("toDataURL worked")

CodePudding user response:

You can override the original function and add your desired code extension at the beginning.

document.createElement = function (create) {
    return function() {
        console.log ('element created');
        return create.apply (this, arguments);
    };
}(document.createElement)

Example adapted from Hooking document.createElement using function prototype

  • Related