用js更改url的方法如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>HTML5无刷修改url - 琼台博客</title>
<script type="text/javascript">
function changeURL(){
var url = document.getElementById('url').value;
window.history.pushState({},0,'http://'+window.location.host+window.location.pathname+url);
}
</script>
</head>
<body>
<h1>html5无刷新改变url</h1>
<div id="info" style="margin:30px 0;">
页面真实地址:
<span style="color:red;"><script type="text/javascript">document.write(window.location.href);</script></span>
</div>
<div>
请输入要改变地URL字符串:<input id='url' type="text" />
<button οnclick="changeURL();">点击无刷改变url</button>
</div>
<div style="color:red;margin-top:30px;">请使用支持html5的浏览器访问</div>
<div style="margin-top:30px;"><a href="http://www.qttc.net/201303292.html" target="_blank">《html5无刷新改变URL》</a> - 琼台博客</div>
</body>
</html>
演示地址:http://www.qttc.net/static/demo/html5_20130320/test.html
转载地址:http://www.qttc.net/201303292.html
location对象属性图示
![location对象属性 location对象属性](https://i-blog.csdnimg.cn/blog_migrate/b1bc7b6d237ec4ed15d68645557b2ab0.png)
location属性
- JavaScript hash 属性 -- 返回URL中#符号后面的内容
- JavaScript host 属性 -- 返回域名
- JavaScript hostname 属性 -- 返回主域名
- JavaScript href 属性 -- 返回当前文档的完整URL或设置当前文档的URL
- JavaScript pathname 属性 -- 返回URL中域名后的部分
- JavaScript port 属性 -- 返回URL中的端口
- JavaScript protocol 属性 -- 返回URL中的协议
- JavaScript search 属性 -- 返回URL中的查询字符串
- JavaScript assign() 函数 -- 设置当前文档的URL
- JavaScript replace() 函数 -- 设置当前文档的URL,并在history对象的地址列表中删除这个URL
- JavaScript reload() 函数 -- 重新载入当前文档(从server服务器端)
- JavaScript toString() 函数 -- 返回location对象href属性当前的值
猴子提示: 主域名是不带www的域名,例如dreamdu.com,主域名前面带前缀的通常都为二级域名或多级域名,例如www.dreamdu.com其实是二级域名。