JavaScript 27 Location

Location表示浏览器中的地址栏

示例 1 : 

刷新当前页面

<span>当前时间:</span>
<script>
  var d = new Date();
  document.write(d.getHours());
  document.write(":");
  document.write(d.getMinutes());
  document.write(":");
  document.write(d.getSeconds());
  document.write(":");
  document.write(d.getMilliseconds());
 
function refresh(){
  location.reload();
}
</script>
 
<br>
<button onclick="refresh()">刷新当前页面</button>

  示例 2 : 

跳转到另一个页面

<script>
function jump(){
  //方法1
  //location="/";
 
  //方法2
  location.assign("/");
   
}
</script>
 
<br>
<button onclick="jump()">跳转到首页</button>

 示例 3 : 

Location的其他属性

<script>
function p(s){
document.write(s);
document.write("<br>");
}

p("协议 location.protocol:"+location.protocol);
p("主机名 location.hostname:"+location.hostname);
p("端口号 (默认是80,没有即表示80端口)location.port:"+location.port);

p("主机加端口号 location.host: "+location.host);
p("访问的路径 location.pathname: "+location.pathname);

p("锚点 location.hash: "+location.hash);
p("参数列表 location.search: "+location.search);

</script>

原文地址:https://www.cnblogs.com/JasperZhao/p/13370124.html