Skip to content Skip to sidebar Skip to footer

How To Set The Event To Be Triggered First?

I want to inject the javascript code to the website by Tampermonkey(a browser plugin which can inject userscript) I want to inject: function temp() {...} window.addEventListener('b

Solution 1:

Add this jquery plugin:

(function ($) {
    $.fn.bindFirst = function(name, fn) {
       this.on(name, fn);
       this.each(function() {
          var handlers = $._data(this, 'events')[name.split('.')[0]];
          var handler = handlers.pop();
          handlers.splice(0, 0, handler);
       });
    };
})(jQuery);

and after that use like below:

window.bindFirst("beforeunload", temp);

Post a Comment for "How To Set The Event To Be Triggered First?"