[ACCEPTED]-Programmatically disable window.location.reload?-javascript

Accepted answer
Score: 10

The problem is that for some reason, location.reload effectively 10 is not a writable property in Firefox and 9 Chrome. Here's some crazy way I came up 8 with to override it (and others) in those 7 browsers. It uses the non-standard .__defineGetter__() method, in 6 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(){});
    return s;
__defineSetter__('location', function(){});

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

Score: 3

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


More Related questions