詳解js location.href和window.open的幾種用法和區(qū)別
一、location.href常見(jiàn)的幾種形式
- self.location.href;//當(dāng)前頁(yè)面打開(kāi)URL頁(yè)面
- window.location.href;//當(dāng)前頁(yè)面打開(kāi)URL頁(yè)面
- this.location.href;//當(dāng)前頁(yè)面打開(kāi)URL頁(yè)面
- location.href;// 當(dāng)前頁(yè)面打開(kāi)URL頁(yè)面
- parent.location.href;//在父頁(yè)面打開(kāi)新頁(yè)面
- top.location.href;//在頂層頁(yè)面打開(kāi)新頁(yè)面
①如果頁(yè)面中自定義了frame,那么可將parent、self、top換為自定義frame的名稱(chēng),效果是在frame窗口打開(kāi)url地址。
②此外,window.location.href=window.location.href;和window.location.Reload();都是刷新當(dāng)前頁(yè)面。
區(qū)別在于是否有提交數(shù)據(jù)。當(dāng)有提交數(shù)據(jù)時(shí),window.location.Reload()會(huì)提示是否提交,window.location.href=window.location.href;則是向指定的url提交數(shù)據(jù).
③用window.open()打開(kāi)新頁(yè)面
但是用window.location.href="" 卻是在原窗口打開(kāi)的.
有時(shí)瀏覽器會(huì)一些安全設(shè)置window.open肯定被屏蔽。例如避免彈出廣告窗口。
二、location.href不同形式之間的區(qū)別
a.html:
<form id="form1" action=""> <div><strong>這是a.html頁(yè)面<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='跳轉(zhuǎn)'> <iframe src="d.html" width="500px" height="300px"></iframe>
a.html里面嵌著b.html;
b.html里面嵌著c.html;
c.html里面嵌著d.html
在d.html里面head部分寫(xiě)js:
function jump() { //經(jīng)測(cè)試:window.location.href與location.href,self.location.href,location.href都是本頁(yè)面跳轉(zhuǎn) //作用一樣 window.location. rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; //location. rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; //self.location. rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; //this.location. rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; //location. rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; }
再次運(yùn)行a.html,點(diǎn)擊那個(gè)"跳轉(zhuǎn)" 按鈕,運(yùn)行結(jié)果貼圖二如下:
對(duì)比圖一和圖二的變化,你會(huì)發(fā)現(xiàn)d.html部分已經(jīng)跳轉(zhuǎn)到了百度的首頁(yè),而其它地方?jīng)]有發(fā)生變化。這也就解釋了"本頁(yè)跳轉(zhuǎn)"是什么意思。
修改d.html里面的js部分為:
function jump() { parent.location.; }
分析:我點(diǎn)擊的是a.html中嵌套的d.html部分的跳轉(zhuǎn)按鈕,結(jié)果是a.html中嵌套的c.html部分跳轉(zhuǎn)到了百度首頁(yè),這就解釋了"parent.location.href是上一層頁(yè)面跳轉(zhuǎn)"的意思。
再次修改d.html里面的js部分為:
function jump() { top.location.; }
運(yùn)行a.html后,再次點(diǎn)擊"跳轉(zhuǎn)" 按鈕,
你會(huì)發(fā)現(xiàn),a.html已經(jīng)跳轉(zhuǎn)到了百度首頁(yè)。
分析:我點(diǎn)擊的是a.html中嵌套的d.html部分的跳轉(zhuǎn)按鈕,結(jié)果是a.html中跳轉(zhuǎn)到了百度首頁(yè),這就解釋了"top.location.href是最外層的頁(yè)面跳轉(zhuǎn)"的意思。
三、location.href總結(jié)
看完上面的講解之后,在來(lái)看看下面的定義你就會(huì)非常明白了:
"top.location.href"是最外層的頁(yè)面跳轉(zhuǎn) "window.location.href"、"location.href"是本頁(yè)面跳轉(zhuǎn) "parent.location.href"是上一層頁(yè)面跳轉(zhuǎn).
location是window對(duì)象的屬性,而所有的網(wǎng)頁(yè)下的對(duì)象都是屬于window作用域鏈中(這是頂級(jí)作用域),所以使用時(shí)是可以省略window。而top是指向頂級(jí)窗口對(duì)象,parent是指向父級(jí)窗口對(duì)象。
四、window.location.href和window.open的區(qū)別
window.location是window對(duì)象的屬性,而window.open是window對(duì)象的方法
window.location是你對(duì)當(dāng)前瀏覽器窗口的URL地址對(duì)象的參考!
window.open是用來(lái)打開(kāi)一個(gè)新窗口的函數(shù)!
window.open不一定是打開(kāi)一個(gè)新窗口!!!!!!!! 只要有窗口的名稱(chēng)和window.open中第二個(gè)參數(shù)中的一樣就會(huì)將這個(gè)窗口替換,用這個(gè)特性的話可以在iframe和frame中來(lái)代替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等做鏈接時(shí)一般都要用Javascript來(lái)完成,和做普通鏈接一樣,可能我們需要讓鏈接頁(yè)面在當(dāng)前窗口打開(kāi),也可能需要在新窗口打開(kāi),這時(shí)我們就可以使用下面兩項(xiàng)之一來(lái)完成:
window.open 用來(lái)打開(kāi)新窗口 window.location 用來(lái)替換當(dāng)前頁(yè),也就是重新定位當(dāng)前頁(yè) 可以用以下來(lái)個(gè)實(shí)例來(lái)測(cè)試一下。 <input type="button" value="新窗口打開(kāi)" onclick="window.open('http://www.google.com')"> <input type="button" value="當(dāng)前頁(yè)打開(kāi)" onclick="window.location='http://www.google.com/'">
window.location或window.open如何指定target?
這是一個(gè)經(jīng)常遇到的問(wèn)題,特別是在用frame框架的時(shí)候
解決辦法:
window.location 改為 top.location 即可在頂部鏈接到指定頁(yè)
或
window.open("你的網(wǎng)址","_top");
5、window.open 用來(lái)打開(kāi)新窗口
window.location 用來(lái)替換當(dāng)前頁(yè),也就是重新定位當(dāng)前頁(yè)
用戶(hù)不能改變document.location(因?yàn)檫@是當(dāng)前顯示文檔的位置)。
window.location本身也是一個(gè)對(duì)象。
但是,可以用window.location改變當(dāng)前文檔 (用其它文檔取代當(dāng)前文檔),而document.location不是對(duì)象。
服務(wù)器重定向后有可能使document.url變動(dòng),但window.location.href指的永遠(yuǎn)是訪問(wèn)該網(wǎng)頁(yè)時(shí)用的URL.
大多數(shù)情況下,document.location和location.href是相同的,但是,當(dāng)存在服務(wù)器重定向時(shí),document.location包含的是已經(jīng)裝載的URL,而location.href包含的則是原始請(qǐng)求的文檔的URL.
6、window.open()是可以在一個(gè)網(wǎng)站上打開(kāi)另外的一個(gè)網(wǎng)站的地址
window.location()是只能在一個(gè)網(wǎng)站中打開(kāi)本網(wǎng)站的網(wǎng)頁(yè)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
用JavaScript做簡(jiǎn)易的購(gòu)物車(chē)的代碼示例
這篇文章主要介紹了用JavaScript做簡(jiǎn)易的購(gòu)物車(chē)的代碼示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10uni-app使用Vite.config.js配置文件的超詳細(xì)教程
這篇文章主要給大家介紹了關(guān)于uni-app使用Vite.config.js配置文件的超詳細(xì)教程,在uniapp開(kāi)發(fā)中,vue.config.js是配置webpack的關(guān)鍵文件之一,也可以說(shuō)是uniapp項(xiàng)目自定義配置的中心,需要的朋友可以參考下2023-12-12layui--select使用以及下拉框?qū)崿F(xiàn)鍵盤(pán)選擇的例子
今天小編就為大家分享一篇layui--select使用以及下拉框?qū)崿F(xiàn)鍵盤(pán)選擇的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09JavaScript或jQuery 獲取option value值方法解析
這篇文章主要介紹了JavaScript或jQuery 獲取option value值方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05javascript實(shí)現(xiàn)自定義滾動(dòng)條效果
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)自定義滾動(dòng)條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08js中通過(guò)getElementsByName訪問(wèn)name集合對(duì)象的方法
下面小編就為大家?guī)?lái)一篇js中通過(guò)getElementsByName訪問(wèn)name集合對(duì)象的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10es6數(shù)組的flat(),flatMap()函數(shù)用法實(shí)例分析
這篇文章主要介紹了es6數(shù)組的flat(),flatMap()函數(shù)用法,結(jié)合實(shí)例形式分析了es6數(shù)組的flat(),flatMap()函數(shù)基本功能、使用方法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04JS實(shí)現(xiàn)兼容火狐及IE iframe onload屬性的遮罩層隱藏及顯示效果
這篇文章主要介紹了JS實(shí)現(xiàn)兼容火狐及IE iframe onload屬性的遮罩層隱藏及顯示效果,涉及javascript事件響應(yīng)及針對(duì)頁(yè)面元素屬性的動(dòng)態(tài)操作相關(guān)技巧,需要的朋友可以參考下2016-08-08