Window Location

Window Location

Window.Location对象在编写时可不使用window这个前缀。

一些例子:

*location.hostname返回web主机的域名

*location.pathname返回当前的路径文件名

*location.port返回web主机的端口(80或443)

*location.protocol返回所使用的web协议(http://或https://)

Window Location Href

location.href属性返回当前页面的URL

实例

返回(当前页面的)整个URL:

document.write(location.href);

以上代码输出为:

http://i.cnblogs.com.cn/js/js_window_location.asp

Window Location Pathname

location.pathname属性返回URL路径名。

实例

返回当前URL的路径名。

document.write(location.pathname);

以上代码输出为:

/js/js_window_location.asp

Window Location Assign

location.assign()方法加载新的文档

实例

加载一个新的文档:

<html>
<head>
<script>
function newDoc()
  {
  window.location.assign("http://www.w3school.com.cn")
  }
</script>
</head>
<body>

<input type="button" value="加载新文档" onclick="newDoc()">

</body>
</html>
原文地址:https://www.cnblogs.com/Strong-stone/p/9707462.html