使用js的同窗必定知道js的location.href的做用是什麼,可是在js中關於location.href的用法究竟有哪幾種,究竟有哪些區別,估計不少人都不知道了。安全
目前在開發中常常要用到的幾種形式有:服務器
1
2
3
4
5
6
|
self.location.href;//當前頁面打開URL頁面
window.location.href;//當前頁面打開URL頁面
this
.location.href;//當前頁面打開URL頁面
location.href;// 當前頁面打開URL頁面
parent.location.href;//在父頁面打開新頁面
top.location.href;//在頂層頁面打開新頁面
|
常常見到的大概有以上幾種形式。框架
注:①若是頁面中自定義了frame,那麼可將parent、self、top換爲自定義frame的名稱,效果是在frame窗口打開url地址。函數 ②此外,window.location.href=window.location.href;和window.location.Reload();都是刷新當前頁面。區別在因而否有提交數據。當有提交數據時,window.location.Reload()會提示是否提交,window.location.href=window.location.href;則是向指定的url提交數據.測試 ③用window.open()打開新頁面
|
那麼,這幾種形式的跳轉究竟有什麼區別呢?this
直接講定義,你確定不會理解透徹,下面我來貼四個html代碼,用實際的例子講解。google
a.html:
1
2
3
4
5
|
<
form
id
=
"form1"
action
=
""
>
<
div
><
strong
>這是a.html頁面<
strong
>
<
iframe
src
=
"b.html"
width
=
"500px"
height
=
"300px"
></
iframe
> </
strong
></
strong
></
div
>
</
form
>
<
pre
>
|
b.html:
1
2
|
<
span
>這是b.html</
span
><
span
id
=
"span1"
></
span
><
br
/>
<
iframe
src
=
"c.html"
width
=
"500px"
height
=
"300px"
></
iframe
>
|
c.html:
1
2
|
<
span
><
strong
>這是c.html:<
strong
></
span
><
span
id
=
"span1"
></
span
><
br
/>
<
iframe
src
=
"d.html"
width
=
"500px"
height
=
"300px"
></
iframe
>
|
d.html:
1
2
3
|
<
span
>這是d.html:</
span
><
span
id
=
"span1"
></
span
><
br
/>
<
input
type
=
'button'
onclick
=
'jump();'
value
=
'跳轉'
>
<
iframe
src
=
"d.html"
width
=
"500px"
height
=
"300px"
></
iframe
>
|
a.html,b.html,c.html,d.html經過iframe給聯繫到了一塊兒,那麼它們有什麼的聯繫呢?
觀察代碼,咱們能夠看出:
a.html裏面嵌着b.html;
b.html裏面嵌着c.html;
c.html裏面嵌着d.html
運行a.html,貼圖一以下:
在d.html裏面head部分寫js:
1
2
3
4
5
6
7
8
9
10
|
<strong>
function
jump()
{
//經測試:window.location.href與location.href,self.location.href,location.href都是本頁面跳轉
//做用同樣
window.location.href=
"http://www.baidu.com"
;
//location.href="http://www.baidu.com";
//self.location.href="http://www.baidu.com";
//this.location.href="http://www.baidu.com";
//location.href="http://www.baidu.com";
} </strong>
|
再次運行a.html,點擊那個"跳轉" 按鈕,運行結果貼圖二以下:
對比圖一和圖二的變化,你會發現d.html部分已經跳轉到了百度的首頁,而其它地方沒有發生變化。這也就解釋了"本頁跳轉"是什麼意思。
好,再來修改d.html裏面的js部分爲:
1
2
3
4
|
function
jump()
{
parent.location.href=
'http://www.baidu.com'
;
}
|
運行a.html後,再次點擊"跳轉" 按鈕,運行結果貼圖三以下:
對比圖一和圖三,你會發現a.html中嵌套的c.html部分已經跳轉到了百度首頁。
分析:我點擊的是a.html中嵌套的d.html部分的跳轉按鈕,結果是a.html中嵌套的c.html部分跳轉到了百度首頁,這就解釋了"parent.location.href是上一層頁面跳轉"的意思。
再次修改d.html裏面的js部分爲:
1
2
3
4
|
function
jump()
{
top.location.href=
'http://www.baidu.com'
;
}
|
運行a.html後,再次點擊"跳轉" 按鈕,
你會發現,a.html已經跳轉到了百度首頁。
分析:我點擊的是a.html中嵌套的d.html部分的跳轉按鈕,結果是a.html中跳轉到了百度首頁,這就解釋了"top.location.href是最外層的頁面跳轉"的意思。
看完上面的講解以後,在來看看下面的定義你就會很是明白了:
"top.location.href"是最外層的頁面跳轉
"window.location.href"、"location.href"是本頁面跳轉
"parent.location.href"是上一層頁面跳轉.
location是window對象的屬性,而全部的網頁下的對象都是屬於window做用域鏈中(這是頂級做用域),因此使用時是能夠省略window。而top是指向頂級窗口對象,parent是指向父級窗口對象。
1. window.location是window對象的屬性,而window.open是window對象的方法 |
2. window.open不必定是打開一個新窗口!!!!!!!! |
3. 在給按鈕、表格、單元格、下拉列表和DIV等作連接時通常都要用Javascript來完成,和作普通連接同樣,可能咱們須要讓連接頁面在當前窗口打開,也可能須要在新窗口打開,這時咱們就可使用下面兩項之一來完成: window.open 用來打開新窗口 window.location 用來替換當前頁,也就是從新定位當前頁 能夠用如下來個實例來測試一下。 <input type="button" value="新窗口打開" onclick="window.open('http://www.google.com')"> <input type="button" value="當前頁打開" onclick="window.location='http://www.google.com/'"> |
4. window.location或window.open如何指定target? 這是一個常常遇到的問題,特別是在用frame框架的時候 解決辦法: window.location 改成 top.location 便可在頂部連接到指定頁 或 window.open("你的網址","_top"); |
5. window.open 用來打開新窗口 window.location 用來替換當前頁,也就是從新定位當前頁 用戶不能改變document.location(由於這是當前顯示文檔的位置)。 window.location自己也是一個對象。 可是,能夠用window.location改變當前文檔 (用其它文檔取代當前文檔),而document.location不是對象。 服務器重定向後有可能使document.url變更,但window.location.href指的永遠是訪問該網頁時用的URL. 大多數狀況下,document.location和location.href是相同的,可是,當存在服務器重定向時,document.location包含的是已經裝載的URL,而location.href包含的則是原始請求的文檔的URL. |
6. window.open()是能夠在一個網站上打開另外的一個網站的地址 而window.location()是隻能在一個網站中打開本網站的網頁 |