Search code examples
javascriptjqueryfirefoxdeprecatedpreventdefault

getPreventDefault() - Is it possible to overwrite, replace or something like that?


I have an application that is so modular and use so much of getPreventDefault(), that would take me days to change it to defaultPrevented in all files.

I'm in Windows 7, so some kind of recursive script is too unsafe to that project.

I tried to make getPreventDefault() use defaultPrevented, something like:

function getPreventDefault(e) {
        return defaultPrevented(e);
}

I tried a lot of others ways, but nothing...

Is it possible to do it? My idea for that is keep using getPreventDefault() without warning, until I finish to fix some issues, and then back to change it.

Simple code to test the remove of warning...

<!DOCTYPE html>
<html>
        <head>
                <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>

                <script>
                        function OnBodyClick (event) {
                                if (event.getPreventDefault) {  // Firefox, Opera, Google Chrome and Safari
                                        if (event.getPreventDefault ()) {
                                                console.log ("The default action is canceled.");
                                        } else {
                                                console.log ("The default action is not canceled.");
                                        }
                                }
                        }
                </script>
        </head>
        <body onclick="OnBodyClick(event)">
        </body>
</html>

Solution

  • You sould be fine with using javascript prototype override

    Object.prototype.getPreventDefault = function (e) {       
        return e.defaultPrevented;
    }
    

    If you would like to overwrite a jQuery function, you can use this code after you loaded jQuery

    (function ($) {    
        $.fn.getPreventDefault = function(e) {
            return e.defaultPrevented;
        };
    })(jQuery);