獲取頁面URL

2021-10-01 19:13:15 字數 601 閱讀 2016

跨域下使用獲取iframe的父頁面url

通常情況下,我們獲取iframe父頁面的url很簡單:parent.location或top.location即可,但前提是它們遵循同源策略。

當iframe與父頁面不屬於同乙個網域名稱時,像上面的獲取會因為安全策略原因而失敗。在nczonline的一篇文章上看到一種方法,使用document.referrer。

方法很簡單,通過parent !=window檢測iframe與父頁面是否同源,當不同源時,使用document.referrer,否則使用parent.location.href或者top.location.href;

function getparenturl() catch (e)  

}return url;

}

當iframe是被動態建立的時候,以上方式依然有效。當然,你也可以使用window.name的方式去做處理。

在頁面獲取url

/** 

* //獲取上一專案根路徑

* @return

*/

function getrootpath()

JS獲取當前頁面頁面URL資訊

url即 統一資源定位符 uniform resource locator,url 完整的url由這幾個部分構成 scheme host port path?query fragment scheme 通訊協議 常用的http,ftp,maito等 設定或獲取url從頭到埠號部分。url windo...

c 獲取當前頁面URl

2 通過js獲取 thisdloc document.location thisurl document.url thishref document.location.href thissloc self.location.href thistloc top.location.href thispl...

獲取當前頁面的URL

window location host 返回url 的主機部分,例如 www.com window location hostname 返回www.com window location href 返回整個url字串 window location pathname 返回 a index.php或...