這篇文章將為大家詳細講解有關JavaScript中location.href和window.open的區別是什么,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
一、location.href常見的幾種形式
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()打開新頁面
但是用window.location.href="" 卻是在原窗口打開的.
有時瀏覽器會一些安全設置window.open肯定被屏蔽。例如避免彈出廣告窗口。
二、location.href不同形式之間的區別
a.html:
<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:
<span>這是b.html</span><span id="span1"></span><br /> <iframe src="c.html" width="500px" height="300px"></iframe>
c.html:
<span><strong>這是c.html:<strong></span><span id="span1"></span><br /> <iframe src="d.html" width="500px" height="300px"></iframe>
d.html:
<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;
b.html里面嵌著c.html;
c.html里面嵌著d.html
在d.html里面head部分寫js:
function jump() { //經測試:window.location.href與location.href,self.location.href,location.href都是本頁面跳轉 //作用一樣 window.location.href="http://www.baidu.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; //location.href="http://www.baidu.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; //self.location.href="http://www.baidu.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; //this.location.href="http://www.baidu.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; //location.href="http://www.baidu.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; }
再次運行a.html,點擊那個"跳轉" 按鈕,運行結果貼圖二如下:
對比圖一和圖二的變化,你會發現d.html部分已經跳轉到了百度的首頁,而其它地方沒有發生變化。這也就解釋了"本頁跳轉"是什么意思。
修改d.html里面的js部分為:
function jump() { parent.location.href='http://www.baidu.com'; }
分析:我點擊的是a.html中嵌套的d.html部分的跳轉按鈕,結果是a.html中嵌套的c.html部分跳轉到了百度首頁,這就解釋了"parent.location.href是上一層頁面跳轉"的意思。
再次修改d.html里面的js部分為:
function jump() { top.location.href='http://www.baidu.com'; }
運行a.html后,再次點擊"跳轉" 按鈕,
你會發現,a.html已經跳轉到了百度首頁。
分析:我點擊的是a.html中嵌套的d.html部分的跳轉按鈕,結果是a.html中跳轉到了百度首頁,這就解釋了"top.location.href是最外層的頁面跳轉"的意思。
三、location.href總結
看完上面的講解之后,在來看看下面的定義你就會非常明白了:
"top.location.href"是最外層的頁面跳轉 "window.location.href"、"location.href"是本頁面跳轉 "parent.location.href"是上一層頁面跳轉.
location是window對象的屬性,而所有的網頁下的對象都是屬于window作用域鏈中(這是頂級作用域),所以使用時是可以省略window。而top是指向頂級窗口對象,parent是指向父級窗口對象。
四、window.location.href和window.open的區別
window.location是window對象的屬性,而window.open是window對象的方法
window.location是你對當前瀏覽器窗口的URL地址對象的參考!
window.open是用來打開一個新窗口的函數!
window.open不一定是打開一個新窗口!!!!!!!! 只要有窗口的名稱和window.open中第二個參數中的一樣就會將這個窗口替換,用這個特性的話可以在iframe和frame中來代替location.href。 如<iframe name="aa"></iframe> <input type=button onclick="window.open('1.htm','aa','')">和 <input type=button onclick="self.frames['aa'].location.href='1.htm'">的效果一樣
在給按鈕、表格、單元格、下拉列表和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/'">
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()是只能在一個網站中打開本網站的網頁
關于JavaScript中location.href和window.open的區別是什么就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。