JavaScript獲取當(dāng)前url根目錄(路徑)
主要用到Location 對(duì)象,包含有關(guān)當(dāng)前 URL 的信息,是 Window 對(duì)象的一個(gè)部分,可通過(guò) window.location 屬性來(lái)訪問(wèn)。
方法一、js獲取項(xiàng)目根路徑的方法
function getRootPath(){
var curPageUrl = window.document.location.href;
var rootPath = curPageUrl.split("http://")[0] + curPageUrl.split("http://")[1].split("/")[0]
+ curPageUrl.split("http://")[1].split("/")[1];
return rootPath;
}
方法二 (window.document.location.href/window.document.location.pathname) ------------轉(zhuǎn)自網(wǎng)絡(luò)
function getRootPath_web() {
//獲取當(dāng)前網(wǎng)址,如: http://localhost:8083/uimcardprj/share/meun.jsp
var curWwwPath = window.document.location.href;
//獲取主機(jī)地址之后的目錄,如: uimcardprj/share/meun.jsp
var pathName = window.document.location.pathname;
var pos = curWwwPath.indexOf(pathName);
//獲取主機(jī)地址,如: http://localhost:8083
var localhostPaht = curWwwPath.substring(0, pos);
//獲取帶"/"的項(xiàng)目名,如:/uimcardprj
var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
return (localhostPaht + projectName);
}
方法三(window.location.pathname/window.location.protocol/window.location.host)
function getRootPath_dc() {
var pathName = window.location.pathname.substring(1);
var webName = pathName == '' ? '' : pathName.substring(0, pathName.indexOf('/'));
if (webName == "") {
return window.location.protocol + '//' + window.location.host;
}
else {
return window.location.protocol + '//' + window.location.host + '/' + webName;
}
}
注:
1、document默示的是一個(gè)文檔對(duì)象,window默示的是一個(gè)窗口對(duì)象,一個(gè)窗口下可以有多個(gè)文檔對(duì)象。
所以一個(gè)窗口下只有一個(gè)window.location.href,然則可能有多個(gè)document.URL、document.location.href------------轉(zhuǎn)自網(wǎng)絡(luò)
2、window.location.href和document.location.href可以被賦值,然后跳轉(zhuǎn)到其它頁(yè)面,document.URL只能讀不克不及寫(xiě)------------轉(zhuǎn)自網(wǎng)絡(luò)
3、Location 對(duì)象詳細(xì)信息參考w3school http://www.dbjr.com.cn/w3school/jsref/dom_obj_location.htm
腳本之家小編補(bǔ)充:
排除某些目錄的廣告實(shí)現(xiàn)
var pathName = window.document.location.pathname;
var projectName = pathName.substring(1, pathName.substr(1).indexOf('/') + 1);
var ad_projectlist = ',,web,html5,css,';
if(ad_projectlist.indexOf(','+projectName+',') < 0){
alert("web,html5,css幾個(gè)目錄代碼不執(zhí)行");
}
以上就是本文的全部?jī)?nèi)容,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript比較當(dāng)前時(shí)間是否在指定時(shí)間段內(nèi)的方法
這篇文章主要介紹了JavaScript比較當(dāng)前時(shí)間是否在指定時(shí)間段內(nèi)的方法,涉及javascript時(shí)間與字符串的轉(zhuǎn)換及比較操作相關(guān)技巧,需要的朋友可以參考下2016-08-08
通過(guò)百度地圖獲取公交線路的站點(diǎn)坐標(biāo)的js代碼
通過(guò)百度地圖獲取公交線路的站點(diǎn)坐標(biāo)的js代碼,需要的朋友可以參考下2012-05-05
javascript實(shí)現(xiàn)點(diǎn)擊圖片切換
這篇文章主要介紹了javascript實(shí)現(xiàn)點(diǎn)擊圖片切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
JavaScript?數(shù)據(jù)結(jié)構(gòu)之集合創(chuàng)建(2)
這篇文章主要介紹了JavaScript?數(shù)據(jù)結(jié)構(gòu)之集合創(chuàng)建,上一篇我們介紹了什么是集合,并且手動(dòng)實(shí)現(xiàn)了一個(gè)集合的類(lèi),本篇基于上篇內(nèi)容繼續(xù)深入介紹需要的小伙伴可以參考一下2022-04-04
JS類(lèi)中定義原型方法的兩種實(shí)現(xiàn)的區(qū)別
JS類(lèi)中定義原型方法的兩種實(shí)現(xiàn)的區(qū)別...2007-03-03
微信小程序開(kāi)發(fā)之從相冊(cè)獲取圖片 使用相機(jī)拍照 本地圖片上傳
本篇文章主要介紹了微信小程序開(kāi)發(fā)之從相冊(cè)獲取圖片--使用相機(jī)拍照,本地圖片上傳的相關(guān)資料。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-04-04

