富贵资源网 Design By www.hznty.com

主要用到Location 对象,包含有关当前 URL 的信息,是 Window 对象的一个部分,可通过 window.location 属性来访问。

方法一、js获取项目根路径的方法

function getRootPath(){
  var curPageUrl = window.document.location.href;
  var rootPath = curPageUrl.split("//")[0] + curPageUrl.split("//")[1].split("/")[0] 
          + curPageUrl.split("//")[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 == '' "") {
  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 https://www.jb51.net/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几个目录代码不执行");
}

以上就是本文的全部内容,希望能给大家一个参考,也希望大家多多支持。

富贵资源网 Design By www.hznty.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
富贵资源网 Design By www.hznty.com

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。