以编程方式禁用window.location.reload? [英] Programmatically disable window.location.reload?

查看:974
本文介绍了以编程方式禁用window.location.reload?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法覆盖window.location.reload的默认行为 - 使其成为无操作,用于调试目的?

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

推荐答案

问题在于,出于某种原因, location.reload 实际上并不是Firefox和Chrome中的可写属性。这是我想出来的一些疯狂方式,在这些浏览器中覆盖它(和其他人)。它使用非标准 .__ defineGetter __() 方法,部分是为了绕过干扰的 window.location =/ home.html的魔力。

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(){});

生成的模拟对象应该阻止任何函数调用(包括 .reload )或赋值(设置 .href )。或者,您可以将测试限制为IE,Safari和Opera,其中 .reload 是可写的。

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.

这篇关于以编程方式禁用window.location.reload?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆