question

GopalDevra avatar image
0 Votes"
GopalDevra asked ·

Error “Cannot redefine non-configurable property 'location'” on window.location in javascript using in Universal Windows app (UAP/UWP)

I had a Windows 8.1 jsproject, now updated to the Universal Windows (UAP/UWP)app.

Previously this function was working, it was calling like

 installLocationProxy(window);
 installLocationProxy(document);

 function installLocationProxy(obj) {
             var descriptor;
             var descriptorProto = obj;
             do {
                 descriptor = Object.getOwnPropertyDescriptor(descriptorProto, "location");
                 descriptorProto = !descriptor && descriptorProto && Object.getPrototypeOf(descriptorProto);
             } while (descriptor == null);
             if (!descriptor || !descriptor.get || !descriptor.set)
                 return;
             var proxy = new LocationProxy(descriptor.get.call(obj));
             Object.defineProperty(obj, "location", {
                 "get": function () {
                     return proxy;
                 },
                 "set": function (location) {
                     if (typeof location === 'string' || location && location instanceof String) {
                         proxy.assign(location, NavigationMethods.Location);
                     }
                     else {
                         return descriptor.set.call(obj, location);
                     }
                 }
             });
         }


but now I am getting error "Cannot redefine non-configurable property 'location'" on this line Object.defineProperty(obj, "location".

While googling I found that the now the window.location is readonly and not writable, so it cannot be overridden.

For reference, it can be checked here https://gist.github.com/zacharytamas/082e538784ebe07e40f9

But I am not sure how to override the window.location property? Can anybody help?

uwpuwp-xamlwindows-runtime
· 3
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

According to the document: Windows.location, the property is read-only. You've already known it, right? So the property can not be overridden. Why do you want to override this property? Are there any special requirements for you?

0 Votes 0 · ·

Are there any updates about this issue?

0 Votes 0 · ·

@RoyLi-MSFT Actually we are setting up custom setter and getter for the same property. In-app with the normal URLs we are supporting the custom URL schemes as well. Like navto://, goto:// etc. So before navigating to a new URL, we find out the type of the protocol from the web URL and then redirect.

So now I am not able to find out the way to find the type of URL or protocol before redirecting the user to the clicked URL. And looking for the alternative for the same. Thanks

0 Votes 0 · ·

1 Answer

RoyLi-MSFT avatar image
0 Votes"
RoyLi-MSFT answered ·

Hello,

Welcome to Microsoft Q&A!

So you are trying to intercept and redirect the navigation, right?

If it's your own site, you just call a JS function, do your logic in it and assign the new URL to the windows.location.

But if it's not your site, it belongs to others, we don't recommend you to do this because of security issues.

· 3 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @RoyLi-MSFT

Thanks for the update, I also tried to update the window.location.href but it is also not supporting in the Edge/IE browser and same is working in the Chrome browser. Does this work like this?

And window.location is working by assigning it href as string.

0 Votes 0 · ·

Yes, as the document said, 'Though Window.location is a read-only Location object, you can also assign a DOMString to it. This means that you can work with location as if it were a string in most cases: location = 'http://www.example.com' is a synonym of location.href = 'http://www.example.com'.' So it should work when you assign a string to the href.

0 Votes 0 · ·

Correct @RoyLi-MSFT, window.location is readonly Location object, now I am trying to override the 'href' property of the window.location, I assume this can be read/write. But I am getting the same error "Cannot redefine non-configurable property 'href'".

Is there any way so I can override a property of the Location object so I can do the same thing which I was doing with window.location object?

Thanks

0 Votes 0 · ·