1. window.location对象可用于获取当前页面地址(URL)并把浏览器重定向到新页面。
2. window.location.href属性返回当前页面的URL。
3. window.location.hostname属性返回(当前页面的)因特网主机的名称。
4. window.location.pathname属性返回当前页面的路径名。
5. window.location.protocol属性返回页面的web协议(http:或https:)。
6. window.location.port属性返回(当前页面的)互联网主机端口的编号。
7. 例子
7.1. 新建一个Web项目
7.2. 编写Location.html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<title>获取当前页面地址</title>
</head>
<body>
<script type="text/javascript">
document.write('页面位置是: ' + window.location.href + '<br />');
document.write('页面主机名是: ' + window.location.hostname + '<br />');
document.write('页面路径是: ' + window.location.pathname + '<br />');
document.write('页面协议是: ' + window.location.protocol + '<br />');
document.write('端口号是: ' + window.location.port + '<br />');
</script>
</body>
</html>
7.3. 效果图