Programmatically disable [removed].reload?

前端 未结 2 719
我寻月下人不归
我寻月下人不归 2020-12-11 04:28

Is there a way to override default behavior of window.location.reload - making it a no-op, for debugging purposes?

相关标签:
2条回答
  • 2020-12-11 05:10

    you have to call this code in a self-calling-function unless it won't work.

    (function(location){
       ...
    })(window.location);
    
    0 讨论(0)
  • 2020-12-11 05:14

    The problem is that for some reason, location.reload effectively is not a writable property in Firefox and Chrome. Here's some crazy way I came up with to override it (and others) in those browsers. It uses the non-standard .__defineGetter__() method, in part to bypass the magic of window.location = "/home.html" from interfering.

    var _location = location;
    __defineGetter__('location', function() {
        var s = new String(_location);
        for(i in _location) (function(i) {
            s.__defineGetter__(i, function() {
                return typeof _location[i] == 'function' ? function(){} : _location[i];
            });
            s.__defineSetter__(i, function(){});
        })(i);
        return s;
    });
    __defineSetter__('location', function(){});
    

    The resulting mock object should prevent any function call (including .reload) or assignment (setting .href) from actually taking effect. Alternatively, you can limit your testing to IE, Safari, and Opera, in which .reload is writable.

    0 讨论(0)
提交回复
热议问题