location对象被包含在window.location属性中,该对象也比较常用在开发过程中 但是也特的简单。下面以一个例子 来说明他的所有的方法跟属性
<html>
<head>
<title>location</title>
</head>
<body>
<script type="text/javascript">
document.write("hash:="+location.hash+"<br/>"); //url中#之后的所有字符串
document.write("host:="+location.host+"<br/>"); //主机ip地址
document.write("hostName:="+location.hostname+"<br/>"); //主机ip地址或者是主机的 名称
document.write("herf:="+location.href+"<br/>"); //当前的url
document.write("pathName:="+location.pathname+"<br/>"); //虚拟目录路径
document.write("search:="+location.search+"<br/>"); //?开始的字符串 要是后面 有#号就节结束,要是 ?在#后面不起作用都算为锚点。
</script>
<input type="button" onclick="location.href='http://baidu.com?seach#hash'" value="跳转到百度页面,并设置seach 与hash"/>
<input type="button" onclick="location.assign('http://baidu.com?seach#hash')" value="当前页加载百度页面"/>
<input type="button" onclick="location.reload()" value="重新加载页面"/>
<input type="button" onclick="location.replace('http://baidu.com?seach#hash')" value="百度替换该页面"/>
</body>
</html>