What is window.origin?

What is window.origin? It doesn't seem to be documented in the usual place.

It looks like it might be very similar to window.location.origin - for example, here on Stack Overflow, both return

https://stackoverflow.com

But inside an iframe, they're different:

console.log(window.location.origin);
console.log(window.origin);

Location origin Property, Definition and Usage. The origin property returns the protocol, hostname and port number of a URL. Note: If the port number is not specified in the URL (or if it is the scheme's default port - like 80, or 443), some browsers will not display the port number. window (n.) c. 1200, literally "wind eye," from Old Norse vindauga, from vindr "wind" (see wind (n.1)) + auga "eye" (from PIE root *okw-"to see"). Replaced Old English eagþyrl, literally "eye-hole," and eagduru, literally "eye-door." Compare Old Frisian andern "window," literally "breath-door." Originally an unglazed hole in a roof.


I think it's not documented because both are the same with little difference, It most cases it doesn't really matter which option you choose to use. There are, however, a few instances where one is preferred over the other, such as:

  1. same-origin policy conflicts if setting a new URL in an iframe or another window that is already of a different origin from the current window (e.g an iframe has a different domain than the document it is loaded in and you want to change the URL of the iframe) would use window. location to set the new URL. This is because location.href is read-only when called from a different origin (domain).
  2. if using use strictly in your JavaScript may cause an exception if using window. location since you are essentially assigning a string to an object, so here it would be best to use the full window.location.href.

See this question for reference.

JavaScript Window Location, property returns the protocol, hostname and port number of a URL. Note: If the port number is not specified in the URL (or if it is the scheme's default port - like 80, or 443), some browsers will not display the port number. Window is first recorded in the early 13th century, and originally referred to an unglazed hole in a roof. Window replaced the Old English eagþyrl, which literally means 'eye-hole,' and 'eagduru' 'eye-door'.


Location describes the URL of the current page. It is available through the window. location property.

<a id='foo' href='#bar'>Go to #bar</a>


<div style='height: 1000px'></div>


<a id='bar' href='#foo'>Go to #foo</a>
<script>
  var printHash = function() {




    console.log("'" + window.location.hash + "'");

  };
  // Print initial hash
  printHash();
  window.onhashchange = printHash;
</script>`enter code here`

More info: Internet Explorer does not have access to window.location.origin, which is a bummer because it is a pretty handy variable to have, but we can make it work with a fairly straight forward check because we access .origin;

if (!window.location.origin) {
  window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '');
}

JavaScript Window Location, What is the purpose of the window location object in JavaScript? Answer What’s the origin of the word window? Early Norse homes were simply designed and often included a stable area for livestock under the same roof as the humans. In the winter, because the tightly shut doors trapped stale air and smoke from the indoor fires, they built holes high on the walls and in the roof for ventilation.


Quite an interesting question! 😊

We can start with some investigation - let's see the output from window

console.log(window)

5.3 Origin, can be used to get the current page address (URL) and to redirect the browser to a new page. The Window family name was found in the USA, the UK, Canada, and Scotland between 1840 and 1920. The most Window families were found in the UK in 1891. In 1840 there were 9 Window families living in Virginia. This was about 69% of all the recorded Window's in the USA.


Location: origin, Examples. // On this page, returns the origin var result = window.location.origin; // Returns:'https://developer.mozilla.org'  1 An opening in the wall or roof of a building or vehicle, fitted with glass in a frame to admit light or air and allow people to see out. ‘The apartments and penthouses have double-glazed redwood framed windows, fitted kitchens and gas-fired central heating.’


Window.location, The Window.location read-only property returns a Location object with information about the current location of the document. an opening in a building, vehicle, or container, for letting in light or air or for looking through, usually having a pane or panes of glass, etc. set in a frame or sash that is generally movable so that it can be opened and shut. an opening, period of time, etc. for access: a window of opportunity.


Cross-window communication, I've been trying to get some of my previous karma tests to pass in node using jsdom and I finally found my issue. The file  The Document.origin read-only property returns the document's origin. In most cases, this property is equivalent to document.defaultView.location.origin. Syntax var origin = document.origin; Examples