该例子通过 location.pathname 属性得到 URL 中的路径部分。
假设当前页面的 URL 是:http://www.520mg.com/test/test.shtml
<script type="text/javascript"> document.write(location.pathname); </script>
运行该例子,输出:
/test/test.shtml
该例子通过 location.pathname 属性来设置 URL 中的路径:
假设当前页面的 URL 是:http://www.520mg.com/test.shtml
<html>
<script type="text/javascript">
function setPathname(){
location.pathname = "/test/test.shtml";
}
</script>
<body>
<button onclick="setPathname()">设定新的路径</button>
</body>
</html>
运行该例子,点击 设定新的路径 按钮,触发 setPathname() 函数,浏览器地址栏的 URL 将变为:http://www.520mg.com/test/test.shtml,而浏览器也将访问该 URL。