适用范围:

  1. 获取和操作URL

示例

// 获取当前页面的URL
console.log(location.href);
 
// 获取当前页面的主机名
console.log(location.hostname);
 
// 获取当前页面的路径部分
console.log(location.pathname);
 
// 改变当前页面的URL,将页面重定向到指定URL
location.href = 'https://www.example.com';