Categories
interview

Window.location object

Javascript can access the URL of the current page with window.location object. This object contains various properties of the URL such as protocol, host, pathname, search and more.

  • window.location.href returns the href (URL) of the current page
  • window.location.hostname returns the domain name of the web host
  • window.location.pathname returns the path and filename of the current page
  • window.location.protocol returns the web protocol used (http: or https:)
  • window.location.assign() loads a new document
<button class="newDocument">
   New Document
</button>
console.log(window.location.href); //"https://fiddle.jshell.net/_display/?editor_console=true"

console.log(window.location.hostname); //"fiddle.jshell.net"

console.log(window.location.pathname); //"/_display/"

console.log(window.location.protocol); //"https:"

//Window.location.assign - click on the "New Document" button to load a new document.

function newDocument() {
  window.location.assign("https://www.collegestash.com/")
}

const newDocButton = document.querySelector(".newDocument");

newDocButton.addEventListener('click', newDocument);

Demo