0

I'm a newbie to js and recently I've learned that we can navigate to a new page in a browser tab by assigning a new value to the window.location object like: window.location = "https://www.stackoverflow.com". But I'm confused about this assign operation. In javascript, a variable changes it's type and value in an assign operation. For example, when running the following program segment, the type of a will be undefined then number and finally string:

var a;
a = 1;
a = "str";

But assigning a string object (i.e. the url) to window.location doesn't change it's type, window.location is still an instance of Location class, only with some of it's attributes changed (e.g. window.location.href, window.location.host etc.). This is just like overloading the "=" operator with the window.location.assign() function, but it seems that javascript doesn't offer an overload mechanism. I have no idea what do browsers (i.e. the javascript running environment) do to achieve this. I would be grateful if anyone can help me with this problem.

5
  • soooo what is problem Commented Sep 18, 2020 at 7:29
  • It is just a setter probably: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Sep 18, 2020 at 7:34
  • @NurbekBoymurodov The problem is when you assign a string object (an url) to window.location , it doesn't become the string object but use the the url to change it's attributes instead, just as window.location.assign() is called. Commented Sep 18, 2020 at 8:24
  • 1
    You didn't choose the most simple object to feed your curiosity ;-) Have a look at that note from the specifcations themselves (yes the normally technical texts that implementers like browsers should follow). Location is actually not a proper JS object, it's a DOM object which gets wrapped into a JS object later. But that's probably more confusing than anything else, so let's pretend it's just a getter like others since you seem interested only by that part of the monster. Commented Sep 18, 2020 at 8:27
  • 1
    @Kaiido Thanks for your help! I spent some time reading the WHATWG standard and then the W3C IDL document, and finally found the answer. The behavior of assigning a url to window.location is defined in the IDL of the window object, where the assign operation to window.location is forwarded to window.location.href. Thank you for offering a clue so that I can find the rest :) Commented Sep 20, 2020 at 3:58

1 Answer 1

1

It's probably a setter https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set in combination with location.assign() ( https://developer.mozilla.org/en-US/docs/Web/API/Location/assign ) being called behind the scenes.

From https://developer.mozilla.org/en-US/docs/Web/API/Window/location : "Whenever a new value is assigned to the location object, a document will be loaded using the URL as if location.assign()".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.