JavaScript 視窗位置
Location 物件
JavaScript Location 物件用於取得或設定目前的 URL 資訊。 Location 物件是 window 物件的一部分,可透過 window.location 屬性進行存取。
Location 物件常用於取得 URL 位址中的信息,或是刷新目前頁面,頁面重定向等,具體可見下面列出的各屬性和方法。
Location 物件屬性
物件location.hash ? ?設定或取得 URL 中的錨? ?
location.host ? ?設定或取得網(wǎng)址 中主機(包括連接埠號碼) ? ?
location.hostname ? ?設定或取得URL 設定或取得的主機名稱主機名稱
location.href ? ?設定或取得完整URL(頁面重新導向應用) ? ?
location.pathname ? ?設定或取得 URL 中的路徑? ?
location.port ? ?設定或取得中的路徑? ?
location.port ? ?設定或取得上的路徑?的連接埠號碼? ?
location.protocol ? ?設定或取得網(wǎng)址使用的協(xié)定? ?
#location.protocol ? ?設定或取得URL 使用的協(xié)定
##Location 物件方法History 物件有以下3 個方法:
location.assign():載入新頁面文件location.reload ():重新載入(重新整理)目前頁面
location.replace():以新的文件取代目前文件
##JavaScript location.href 屬性Location 物件的href 屬性用於設定或取得目前完整的URL,語法如下:
##location.href = URL
location.href 屬性最常用於在JavaScript 中進行頁面的跳躍(重定向)返回(目前頁面的)整個URL:<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script type="text/javascript"> document.write(location.href); </script> </head> <body> </body> </html>
JavaScript location.pathname 屬性
#Location 物件的pathname 屬性用於設定或取得目前URL 的路徑部分,語法如下:
location.pathname = path
傳回目前URL 的路徑名稱:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script type="text/javascript"> document.write(location.pathname); </script> </head> <body> </body> </html>
JavaScript location.assign() 方法
Location 物件的assign() 方法用於載入一個新的文檔,語法如下:
location.assign(URL)
#載入一個新的文檔:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script type="text/javascript"> function setAssign(){ window.location.assign("http://ipnx.cn"); } </script> </head> <body> <button onclick="setAssign()">加載新文檔</button> </body> </html>