美文网首页
3 js之Window Location

3 js之Window Location

作者: 一枝妖孽 | 来源:发表于2018-06-01 15:25 被阅读0次

window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面。

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

一些例子:
location.hostname 返回 web 主机的域名
location.pathname 返回当前页面的路径和文件名
location.port 返回 web 主机的端口 (80 或 443)
location.protocol 返回所使用的 web 协议(http:// 或 https://)

2 Window Location Href
location.href 属性返回当前页面的 URL。

3 Window Location Pathname
location.pathname 属性返回 URL 的路径名。

4 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>

5

// 获取根路径
function getRootPath() {
    var curWwwPath = window.document.location.href;
    var pathName = window.document.location.pathname;
    var pos = curWwwPath.indexOf(pathName);
    var localhostPaht = curWwwPath.substring(0, pos);
    var projectName = pathName
            .substring(0, pathName.substr(1).indexOf('/') + 1);
    return (localhostPaht + projectName);
}

相关文章

网友评论

      本文标题:3 js之Window Location

      本文链接:https://www.haomeiwen.com/subject/oukksftx.html