JS to get page URL information summary

  • window.location.href (set or get the entire URL as a string)
var url = window.location.href;
alert(url);
//返回:整个路径
  • window.location.protocol (set or get the protocol part of the URL)
var url = window.location.protocol;
alert(url);
//返回:http或者https
  • window.location.host (set or get the host part of the URL)
var url = window.location.host;
alert(url);
//返回:http://localhost
  • window.location.port (set or get the port number associated with the URL)
var url = window.location.port;
alert(url);
//返回:8080
  • window.location.pathname (set or get with the path part of the URL)
var url = window.location.pathname;
alert(url);
//返回:/user/login
  • window.location.search (sets or gets the part of the href attribute that follows the question mark)
ar url = window.location.search;
alert(url);
//返回:?userName=hyman&age=24
  • window.location.hash (sets or gets the segment after the pound sign "#" in the href attribute)
var url = window.location.hash;
alert(url);
//返回:空字符(因为url中没有)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326001013&siteId=291194637