天天看點

JavaScript的location對象實作跳轉、重新整理頁面

Location 對象

Location 對象是 Window 對象的一個部分,可通過 window.location 屬性來通路。
Location 對象的屬性
屬性 描述
hash 設定或傳回從井号 (#) 開始的 URL(錨)。
host 設定或傳回主機名和目前 URL 的端口号。
hostname 設定或傳回目前 URL 的主機名。
href 設定或傳回完整的 URL。
pathname 設定或傳回目前 URL 的路徑部分。
port 設定或傳回目前 URL 的端口号。
protocol 設定或傳回目前 URL 的協定。
search 設定或傳回從問号 (?) 開始的 URL(查詢部分)。

Location 對象的方法

方法 描述
assign() 加載新的文檔。
reload() 重新加載目前文檔。
replace() 用新的文檔替換目前文檔。

例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title> loaction </title>
  <meta charset="utf-8" />
  <meta name="generator" content="editplus" />
  <meta name="author" content="" />
  <meta name="keywords" content="" />
  <meta name="description" content="" />
 </head>
<script type="text/javascript">
<!--
    function func(){
        window.location.href = "http://www.baidu.com";
    }
    function func1(){
        window.location.reload;
    }
//-->
</script>
 <body >
    <input type="button" value="進入百度" onclick="func()" />
    <input type="button" value="重新整理頁面" onclick="func1()" />
 </body>
</html>
           
以上内容作為個人學習記錄,僅供參考。

繼續閱讀