橘子味的心
标题:JavaScript window location


window.location 对象可用于获取当前页地址(URL),并将浏览器重定向到新页.


Window Location

window.location 对象可以不用窗口window前缀编写.

一些实例:

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

Window Location Href

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

document.getElementById("demo").innerHTML =
"Page location is " + window.location.href;

让我试试


Window Location Hostname

window.location.hostname 属性返回Internet主机(当前页)的名称。

document.getElementById("demo").innerHTML =
"Page hostname is " + window.location.hostname;

让我试试


Window Location Pathname

window.location.pathname 属性返回当前页面的路径.

document.getElementById("demo").innerHTML =
"Page path is " + window.location.pathname;

让我试试


Window Location Protocol

window.location.protocol 属性返回网页的web协议.

document.getElementById("demo").innerHTML =
"Page protocol is " + window.location.protocol;

让我试试


Window Location Assign

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

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

<input type="button" value="Load new document" onclick="newDoc()">

</body>
</html>

让我试试


目录

分类