欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

javascript中window.location.href的用法

 更新時(shí)間:2025年04月26日 16:11:51   作者:網(wǎng)絡(luò)真危險(xiǎn)?。? 
window.location.href?是一個(gè)用于獲取當(dāng)前頁(yè)面?URL?或讓瀏覽器跳轉(zhuǎn)到新?URL?的重要方法,本文就詳細(xì)的介紹一下javascript中window.location.href的用法,感興趣的可以了解一下

一、前言

window.location.href 是一個(gè)用于獲取當(dāng)前頁(yè)面 URL 或讓瀏覽器跳轉(zhuǎn)到新 URL 的重要方法,是 window.location 對(duì)象的屬性。它返回一個(gè)字符串,表示當(dāng)前頁(yè)面的 URL;同時(shí),當(dāng)通過(guò)將 URL 指定給 window.location.href 時(shí),可以讓瀏覽器跳轉(zhuǎn)到新的 URL。

二、常見用例

  • self.location.href="/url" 當(dāng)前頁(yè)面打開URL頁(yè)面
  • location.href="/url :當(dāng)前頁(yè)面打開URL頁(yè)面
  • window.location.href="/url" :當(dāng)前頁(yè)面打開URL頁(yè)面,前面三個(gè)用法相同。
  • this.location.href="/url" :當(dāng)前頁(yè)面打開URL頁(yè)面
  • parent.location.href="/url" :在父頁(yè)面打開新頁(yè)面
  • top.location.href="/url" :在頂層頁(yè)面打開新頁(yè)面
  • 如果頁(yè)面中自定義了 frame,那么可將parent self top換為自定義frame的名稱,效果是在frame窗口打開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ù)

  • 如果要關(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()
  • 如果無(wú)法關(guān)閉當(dāng)前彈框 說(shuō)明可能有父節(jié)點(diǎn),可以試試:window.parent.close();

三、window.location.href和window.open的區(qū)別

1、window.location是window對(duì)象的屬性,而window.open是window對(duì)象的方法 
window.location是你對(duì)當(dāng)前瀏覽器窗口的URL地址對(duì)象的參考!   
window.open是用來(lái)打開一個(gè)新窗口的函數(shù)! 

2、window.open不一定是打開一個(gè)新窗口!!!!!!!!   
只要有窗口的名稱和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'">

的效果一樣 

3、在給按鈕、表格、單元格、下拉列表和DIV等做鏈接時(shí)一般都要用Javascript來(lái)完成,和做普通鏈接一樣,可能我們需要讓鏈接頁(yè)面在當(dāng)前窗口打開,也可能需要在新窗口打開,這時(shí)我們就可以使用下面兩項(xiàng)之一來(lái)完成:

window.open 用來(lái)打開新窗口 
window.location 用來(lái)替換當(dāng)前頁(yè),也就是重新定位當(dāng)前頁(yè) 
可以用以下來(lái)個(gè)實(shí)例來(lái)測(cè)試一下。 

<input type="button" value="新窗口打開" onclick="window.open('http://www.google.com')"> 
<input type="button" value="當(dāng)前頁(yè)打開" onclick="window.location='http://www.google.com/'"> 

4、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)打開新窗口 
window.location 用來(lái)替換當(dāng)前頁(yè),也就是重新定位當(dāng)前頁(yè) 

用戶不能改變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)站上打開另外的一個(gè)網(wǎng)站的地址 
window.location()是只能在一個(gè)網(wǎng)站中打開本網(wǎng)站的網(wǎng)頁(yè) 

 到此這篇關(guān)于javascript中window.location.href的用法   的文章就介紹到這了,更多相關(guān)javascript window.location.href內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論