javascript中window.location.href的用法
一、前言
window.location.href
是一個用于獲取當前頁面 URL 或讓瀏覽器跳轉到新 URL 的重要方法,是 window.location
對象的屬性。它返回一個字符串,表示當前頁面的 URL;同時,當通過將 URL 指定給 window.location.href 時,可以讓瀏覽器跳轉到新的 URL。
二、常見用例
- self.location.href="/url" 當前頁面打開URL頁面
- location.href="/url :當前頁面打開URL頁面
- window.location.href="/url" :當前頁面打開URL頁面,前面三個用法相同。
- this.location.href="/url" :當前頁面打開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() : 刷新當前頁面。
區(qū)別在于是否有提交數(shù)據(jù)。
當有提交數(shù)據(jù)時,window.location.Reload()會提示是否提交,window.location.href=window.location.href;則是向指定的url提交數(shù)據(jù)
- 如果要關閉當前窗口,并且在新窗口打開某一鏈接:
var a = document.createElement('a') a.setAttribute('href', href) a.setAttribute('target', '_blank') a.setAttribute('id', 'startTelMedicine') a.onclick = function () { //關閉窗口的方法 window.opener = null window.open('', '_self', '') window.close() } // 防止反復添加 if (document.getElementById('startTelMedicine')) { document.body.removeChild(document.getElementById('startTelMedicine')) } document.body.appendChild(a) a.click()
- 如果無法關閉當前彈框 說明可能有父節(jié)點,可以試試:window.parent.close();
三、window.location.href和window.open的區(qū)別
1、window.location是window對象的屬性,而window.open是window對象的方法
window.location是你對當前瀏覽器窗口的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來完成,和做普通鏈接一樣,可能我們需要讓鏈接頁面在當前窗口打開,也可能需要在新窗口打開,這時我們就可以使用下面兩項之一來完成:
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.
大多數(shù)情況下,document.location和location.href是相同的,但是,當存在服務器重定向時,document.location包含的是已經裝載的URL,而location.href包含的則是原始請求的文檔的URL.
6、window.open()是可以在一個網站上打開另外的一個網站的地址
window.location()是只能在一個網站中打開本網站的網頁
到此這篇關于javascript中window.location.href的用法 的文章就介紹到這了,更多相關javascript window.location.href內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- js中window.location.href的用法大全
- JavaScript Window 打開新窗口(window.location.href、window.open、window.showModalDialog)
- js獲取當前頁的URL與window.location.href簡單方法
- javascript 中設置window.location.href跳轉無效問題解決辦法
- 快速解決js中window.location.href不工作的問題
- 關于js中window.location.href,location.href,parent.location.href,top.location.href的用法與區(qū)別
相關文章
JavaScript 數(shù)組去重并統(tǒng)計重復元素出現(xiàn)的次數(shù)實例
下面小編就為大家分享一篇JavaScript 數(shù)組去重并統(tǒng)計重復元素出現(xiàn)的次數(shù)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12js的.innerHTML = ""IE9下顯示有錯誤的解決方法
js的.innerHTML= "……"在ie9- 的版本顯示不正常,使用jquery可以解決,有類似問題的朋友可以參考下2013-09-09基于JavaScript實現(xiàn)評論框展開和隱藏功能
本文通過實例代碼給大家介紹了基于JavaScript實現(xiàn)評論框展開和隱藏功能,感興趣的朋友參考下吧2017-08-08JavaScript實現(xiàn)左右下拉框動態(tài)增刪示例
本篇文章主要介紹了JavaScript實現(xiàn)左右下拉框動態(tài)增刪示例,可以對下拉框進行刪除和增加,非常具有實用價值,需要的朋友可以參考下。2017-03-03基于JS代碼實現(xiàn)簡單易用的倒計時 x 天 x 時 x 分 x 秒效果
這篇文章主要介紹了基于JS代碼實現(xiàn)簡單易用的倒計時 x 天 x 時 x 分 x 秒效果,需要的朋友可以參考下2017-07-07