简体   繁体   English

javascript window.location假回调

[英]javascript window.location fake callback

I need to make a fake window.location = "testCall" call in order to generate an event to bypass parameters on a mobile device. 我需要进行伪造的window.location = "testCall"调用,以生成一个事件来绕过移动设备上的参数。 Works as native, however, I need then to dissmiss a NotFound exception or mainly dissmiss a fake window.location call. 作为本机工作,但是,我需要关闭一个NotFound异常或主要关闭一个假的window.location调用。 Possible? 可能? Thank you 谢谢

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

in Chrome and Safari (and I presume in other browsers). 在Chrome和Safari中(我假设在其他浏览器中)。 So seems like you can't change the native behavior. 因此,似乎您无法更改本机行为。

If it behaved as a normal EcmaScript 5 property and configurable was set to true than you could have done something like that: 如果它表现为正常的Ec​​maScript 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);

I hope browser vendors migrate all their magical properties and objects to standard EcmaScript mechanics but at the moment we are out of luck. 我希望浏览器供应商将其所有神奇的属性和对象迁移到标准的EcmaScript机制,但目前我们不走运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM