Jaume
Jaume

Reputation: 3780

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. Works as native, however, I need then to dissmiss a NotFound exception or mainly dissmiss a fake window.location call. Possible? Thank you

Upvotes: 2

Views: 1668

Answers (1)

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

in Chrome and Safari (and I presume in other browsers). 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:

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.

Upvotes: 6

Related Questions