国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JavaScript獲取當前url根目錄(路徑)

2019-11-20 09:40:24
字體:
來源:轉載
供稿:網友

主要用到Location 對象,包含有關當前 URL 的信息,是 Window 對象的一個部分,可通過 window.location 屬性來訪問。

方法一、js獲取項目根路徑的方法

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) ------------轉自網絡

function getRootPath_web() { //獲取當前網址,如: http://localhost:8083/uimcardprj/share/meun.jsp var curWwwPath = window.document.location.href; //獲取主機地址之后的目錄,如: uimcardprj/share/meun.jsp var pathName = window.document.location.pathname; var pos = curWwwPath.indexOf(pathName); //獲取主機地址,如: http://localhost:8083 var localhostPaht = curWwwPath.substring(0, pos); //獲取帶"/"的項目名,如:/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默示的是一個文檔對象,window默示的是一個窗口對象,一個窗口下可以有多個文檔對象。
所以一個窗口下只有一個window.location.href,然則可能有多個document.URL、document.location.href------------轉自網絡

2、window.location.href和document.location.href可以被賦值,然后跳轉到其它頁面,document.URL只能讀不克不及寫------------轉自網絡

3、Location 對象詳細信息參考w3school //m.survivalescaperooms.com/w3school/jsref/dom_obj_location.htm

武林網小編補充:

排除某些目錄的廣告實現

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幾個目錄代碼不執行");}

以上就是本文的全部內容,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 涿鹿县| 广东省| 汉源县| 顺昌县| 贵港市| 扎囊县| 额济纳旗| 普兰县| 那曲县| 沂源县| 织金县| 大港区| 灵石县| 临潭县| 安宁市| 耒阳市| 东港市| 舒兰市| 尚志市| 通渭县| 弥渡县| 岳阳市| 九江县| 兴安盟| 西华县| 天台县| 大姚县| 漳州市| 祁连县| 洮南市| 西丰县| 平泉县| 万荣县| 河曲县| 巍山| 嵩明县| 治多县| 桂平市| 独山县| 黔西县| 昭平县|