javascript window.location fake callback

javascript window.location fake callback

本文关键字:callback fake location window javascript      更新时间:2023-09-26

我需要进行虚假的window.location = "testCall"调用才能生成事件以绕过移动设备上的参数。作为本机工作,但是,我需要消除 NotFound 异常或主要消除假 window.location 调用。可能?谢谢

Object.getOwnPropertyDescriptor(window, 'location').configurable === false

在Chrome和Safari中(我假设在其他浏览器中)。所以似乎你不能改变原生行为。

如果它的行为是正常的 EcmaScript 5 属性,并且configurable设置为 true,那么您可以执行以下操作:

var descriptor = Object.getOwnPropertyDescriptor(window, 'location');
var setter = descriptor.set; // Doesn't exist although it should in spirit of ES5
descriptor.set = function (newLocation) {
    try {
        setter(newLocation);
    } catch (e) {
        console.log('Location error: ', newLocation, e);
    }
};
// The line below will throw exception in real browser :(
// TypeError: Cannot redefine property: location
Object.defineProperty(window, 'location', descriptor);

我希望浏览器供应商将他们所有的神奇属性和对象迁移到标准的 EcmaScript 机制中,但目前我们并不走运。