javascript中window.location.href的用法
一、前言
window.location.href 是一個用于獲取當(dāng)前頁面 URL 或讓瀏覽器跳轉(zhuǎn)到新 URL 的重要方法,是 window.location 對象的屬性。它返回一個字符串,表示當(dāng)前頁面的 URL;同時,當(dāng)通過將 URL 指定給 window.location.href 時,可以讓瀏覽器跳轉(zhuǎn)到新的 URL。
二、常見用例
- self.location.href="/url" 當(dāng)前頁面打開URL頁面
- location.href="/url :當(dāng)前頁面打開URL頁面
- window.location.href="/url" :當(dāng)前頁面打開URL頁面,前面三個用法相同。
- this.location.href="/url" :當(dāng)前頁面打開URL頁面
- parent.location.href="/url" :在父頁面打開新頁面
- top.location.href="/url" :在頂層頁面打開新頁面
- 如果頁面中自定義了 frame,那么可將parent self top換為自定義frame的名稱,效果是在frame窗口打開url地址
- window.location.href=window.location.href;和window.location.Reload() : 刷新當(dāng)前頁面。
區(qū)別在于是否有提交數(shù)據(jù)。
當(dāng)有提交數(shù)據(jù)時,window.location.Reload()會提示是否提交,window.location.href=window.location.href;則是向指定的url提交數(shù)據(jù)
- 如果要關(guān)閉當(dāng)前窗口,并且在新窗口打開某一鏈接:
var a = document.createElement('a')
a.setAttribute('href', href)
a.setAttribute('target', '_blank')
a.setAttribute('id', 'startTelMedicine')
a.onclick = function () {
//關(guān)閉窗口的方法
window.opener = null
window.open('', '_self', '')
window.close()
}
// 防止反復(fù)添加
if (document.getElementById('startTelMedicine')) {
document.body.removeChild(document.getElementById('startTelMedicine'))
}
document.body.appendChild(a)
a.click()- 如果無法關(guān)閉當(dāng)前彈框 說明可能有父節(jié)點,可以試試:window.parent.close();
三、window.location.href和window.open的區(qū)別
1、window.location是window對象的屬性,而window.open是window對象的方法
window.location是你對當(dāng)前瀏覽器窗口的URL地址對象的參考!
window.open是用來打開一個新窗口的函數(shù)!
2、window.open不一定是打開一個新窗口!!!!!!!!
只要有窗口的名稱和window.open中第二個參數(shù)中的一樣就會將這個窗口替換,用這個特性的話可以在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'">的效果一樣
3、在給按鈕、表格、單元格、下拉列表和DIV等做鏈接時一般都要用Javascript來完成,和做普通鏈接一樣,可能我們需要讓鏈接頁面在當(dāng)前窗口打開,也可能需要在新窗口打開,這時我們就可以使用下面兩項之一來完成:
window.open 用來打開新窗口
window.location 用來替換當(dāng)前頁,也就是重新定位當(dāng)前頁
可以用以下來個實例來測試一下。
<input type="button" value="新窗口打開" onclick="window.open('http://www.google.com')">
<input type="button" value="當(dāng)前頁打開" onclick="window.location='http://www.google.com/'"> 4、window.location或window.open如何指定target?
這是一個經(jīng)常遇到的問題,特別是在用frame框架的時候
解決辦法:
window.location 改為 top.location 即可在頂部鏈接到指定頁
或
window.open("你的網(wǎng)址","_top"); 5、window.open 用來打開新窗口
window.location 用來替換當(dāng)前頁,也就是重新定位當(dāng)前頁
用戶不能改變document.location(因為這是當(dāng)前顯示文檔的位置)。
window.location本身也是一個對象。
但是,可以用window.location改變當(dāng)前文檔 (用其它文檔取代當(dāng)前文檔),而document.location不是對象。
服務(wù)器重定向后有可能使document.url變動,但window.location.href指的永遠(yuǎn)是訪問該網(wǎng)頁時用的URL.
大多數(shù)情況下,document.location和location.href是相同的,但是,當(dāng)存在服務(wù)器重定向時,document.location包含的是已經(jīng)裝載的URL,而location.href包含的則是原始請求的文檔的URL.
6、window.open()是可以在一個網(wǎng)站上打開另外的一個網(wǎng)站的地址
window.location()是只能在一個網(wǎng)站中打開本網(wǎng)站的網(wǎng)頁
到此這篇關(guān)于javascript中window.location.href的用法 的文章就介紹到這了,更多相關(guān)javascript window.location.href內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- javascript 中設(shè)置window.location.href跳轉(zhuǎn)無效問題解決辦法
- JavaScript Window 打開新窗口(window.location.href、window.open、window.showModalDialog)
- js獲取當(dāng)前頁的URL與window.location.href簡單方法
- 關(guān)于js中window.location.href,location.href,parent.location.href,top.location.href的用法與區(qū)別
- 快速解決js中window.location.href不工作的問題
- js中window.location.href的用法大全
- JS操作BOM時window.location.href跳轉(zhuǎn)后頁面不刷新問題解決辦法
相關(guān)文章
關(guān)于JavaScript的內(nèi)存與性能問題解決匯總
這篇文章主要介紹了關(guān)于JavaScript的內(nèi)存與性能問題解決匯總,在JavaScript中,頁面中事件處理程序的數(shù)量與頁面整體性能直接相關(guān),原因有很多,下面就一起來看看具體的總結(jié)吧2022-04-04
js時間戳轉(zhuǎn)yyyy-MM-dd HH-mm-ss工具類詳解
這篇文章主要介紹了js時間戳轉(zhuǎn)yyyy-MM-dd HH-mm-ss工具類,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
createElement動態(tài)創(chuàng)建HTML對象腳本代碼
利用createElement動態(tài)創(chuàng)建鏈接,div等代碼2008-11-11
JavaScript中prompt()函數(shù)的用法實戰(zhàn)例子
JavaScript中的prompt是一個函數(shù),用于在瀏覽器中顯示一個對話框,提示用戶輸入一些信息,這篇文章主要給大家介紹了關(guān)于JavaScript中prompt()函數(shù)的用法實戰(zhàn),需要的朋友可以參考下2023-11-11
NestJS+TypeScript 全棧項目骨架配置實戰(zhàn)
對前端轉(zhuǎn)全棧來說,NestJS + TypeScript 是零語言切換成本、快速落地的最優(yōu)解,本文就來詳細(xì)的介紹一下NestJS+TypeScript 全棧項目骨架配置實戰(zhàn),感興趣的可以了解一下2026-04-04
原生javascript AJAX 三級聯(lián)動的實現(xiàn)代碼
這篇文章主要介紹了原生javascript AJAX 三級聯(lián)動的實現(xiàn)代碼,非常不錯代碼簡單易懂,具有一定的參考借鑒價值,需要的朋友可以參考下2018-05-05

