Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

javascript - Programmatically disable window.location.reload?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...