国产探花免费观看_亚洲丰满少妇自慰呻吟_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 //www.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幾個目錄代碼不執行");}

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 峨眉山市| 洞头县| 罗源县| 华容县| 温泉县| 崇信县| 潼南县| 邵阳县| 井研县| 呼图壁县| 灵山县| 刚察县| 邯郸县| 叶城县| 胶南市| 福建省| 抚顺市| 遵义市| 鹤庆县| 龙海市| 城固县| 平遥县| 万载县| 旬阳县| 文昌市| 郑州市| 象州县| 福泉市| 错那县| 龙山县| 鹰潭市| 阜城县| 奇台县| 安徽省| 武山县| 星子县| 齐齐哈尔市| 简阳市| 绥江县| 石渠县| 泰兴市|