How to Get Current Page URL Information Using JavaScript

javascript urlwindow.locationget current page urljavascript domainurl parameters
Published·Modified·
  1. Set or get the file name or path of the specified object.
alert(window.location.pathname);
  1. Set or get the entire URL as a string.
alert(window.location.href);
  1. Set or get the port number associated with the URL.
alert(window.location.port);
  1. Set or get the protocol part of the URL.
alert(window.location.protocol);
  1. Set or get the fragment following the hash symbol ("#") in the href attribute.
alert(window.location.hash);
  1. Set or get the hostname and port number of the location or URL.
alert(window.location.host);
  1. Set or get the part following the question mark in the href attribute.
alert(window.location.search);
  1. Get the value of a variable (extract the part after the equals sign).
var url = window.location.search;
// alert(url.length);
// alert(url.lastIndexOf('='));
var loc = url.substring(url.lastIndexOf('=') + 1, url.length);
  1. Get the domain name of the current webpage.
var domain = document.domain;

Original source: How to Get Current Page URL Information Using JavaScript (JS Get Current Website Information). All rights reserved by the original author. If there is any infringement, please contact QQ: 337003006 for deletion.