JavaScript Window Location

孤者浪人 提交于 2020-02-27 09:05:58

window.location对象可用于获取当前页面地址(url)并把浏览器重定向到新页面。

window.location 对象可不带 window 前缀书写。

一些例子:

  • window.location.href 返回当前页面的 href (URL)
  • window.location.hostname 返回 web 主机的域名
  • window.location.pathname 返回当前页面的路径或文件名
  • window.location.protocol 返回使用的 web 协议(http: 或 https:)
  • window.location.assign 加载新文档

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

实例

加载新文档:

<!DOCTYPE html>
<html>
<body>

<h1>window.location 对象</h1>

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

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

</body>
</html>

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!