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

javascript中window.location.href的用法

 更新時間:2025年04月26日 16:11:51   作者:網絡真危險??!  
window.location.href?是一個用于獲取當前頁面?URL?或讓瀏覽器跳轉到新?URL?的重要方法,本文就詳細的介紹一下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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論