location
对象表示当前页面的URL信息。例如,一个完整的URL:
http://www.example.com:8080/path/index.html?a=1&b=2#TOP
可以用location.href
获取。要获得URL各个部分的值,可以这么写:
location.protocol; // ‘http‘ location.host; // ‘ location.port; // ‘8080‘ location.pathname; // ‘/path/index.html‘ location.search; // ‘?a=1&b=2‘ location.hash; // ‘TOP‘
要加载一个新页面,可以调用location.assign()
。如果要重新加载当前页面,调用location.reload()
方法非常方便。
<a onclick="location.assign(‘http://www.baidu.com‘);">加载一个新页面</a> <a onclick="location.reload();">重新加载当前页面</a>
原文:http://ansue.blog.51cto.com/9481468/1729208