1. 程式人生 > 實用技巧 >JavaScript 27 Location

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>