得到Js文件的绝对路径或相对路径的两法

方法一:

/**
 * 全局函数
 * 得到给定Js文件的绝对路径
 * 可通过这个绝对路径在js文件中引用相对它的其它文件路径
 * c:/aa.html,(在此文件中引入aa.js:<script src='aa.js'></script>)
 * c:/aa.js
 * c:/bb/
 * c:/bb/cc.js
 * 如在aa.html中使用getPath("aa.js") + "../bb/cc.js",这样便是指向到与aa.js同级的目录bb下的cc.js文件
 */
SEditorAPI.getJsFileAbsPath = function(jsFileName){
 var e = {};
 var htmlPath = "";
 var jsPath = "";
 if ( document.location.protocol == 'file:'){
  e.BasePath = unescape(document.location.pathname.substr(1) ) ;
  e.BasePath = e.BasePath.replace( gi, '/' ) ;
  e.BasePath = 'file://' + e.BasePath.substring(0,e.BasePath.lastIndexOf('/')+1) ;
  e.FullBasePath = e.BasePath ;
 }else{
  e.BasePath = document.location.pathname.substring(0,document.location.pathname.lastIndexOf('/')+1) ;
  e.FullBasePath = document.location.protocol + '//' + document.location.host + e.BasePath ;
 }

 htmlPath = e.FullBasePath; 
 var scriptTag = document.getElementsByTagName("script");
 for(var i=0;i<scriptTag.length;i++){
  if(scriptTag[i].src.lastIndexOf(jsFileName) >= 0){   
   var src = scriptTag[i].src.replace( gi, '/') ;//把/转换为/
   if(src.toLowerCase().indexOf("file://")==0){//http全路径形式 file://
    var _temp = src.substring(0,src.lastIndexOf('/')+1);
    jsPath = _temp;
    //alert("file://")
   }else if(src.toLowerCase().indexOf("http://")==0){//http全路径形式 http://
    var _temp = src.substring(0,src.lastIndexOf('/')+1);
    jsPath = _temp;    
    //alert("http://")
   }else if(src.toLowerCase().indexOf("https://")==0){//http全路径形式 https://
    var _temp = src.substring(0,src.lastIndexOf('/')+1);
    jsPath = _temp;    
    //alert("https://")
   }else if(src.toLowerCase().indexOf("../")==0){//相对路径形式 ../
    jsPath = htmlPath + src.substring(0,src.lastIndexOf('/')+1);
    //alert("../")
   }else if(src.toLowerCase().indexOf("./")==0){//相对路径形式 ./
    jsPath = htmlPath + src.substring(0,src.lastIndexOf('/')+1);
    //alert("./")
   }else if(src.toLowerCase().indexOf("/")==0){//相对路径形式 /,只有采用http访问时有效
    if(document.location.protocol == 'http:' || document.location.protocol == 'https:'){
     var _temp = document.location.protocol + "//" + document.location.host + src.substring(0,src.lastIndexOf('/')+1);
     jsPath = _temp;
    }
    //alert("/")
   }else if(src.toLowerCase().search(/^([a-z]{1}):/) >=0){//盘符形式 c:
    var _temp = src.substring(0,src.lastIndexOf('/')+1);
    jsPath = _temp;
    //alert("^([a-z]+):")
   }else{//同级形式
    jsPath = htmlPath;
   }
  }
 }
 return jsPath;
}

 

----------------------------------------------------

方法二,在看了梅花雪的代码后提取出来的。很佩服梅花雪的Js

在js文件中加入如下代码(不要放在任何方法内,原因是只有在加裁到这个文件时t[t.length-1]指向的才是当前的script标签(精华就在这句上)):

var System={};
var t=document.getElementsByTagName("SCRIPT");
t=(System.scriptElement=t[t.length-1]).src.replace(g, "/");
System.path=(t.lastIndexOf("/")<0)?".":t.substring(0, t.lastIndexOf("/"));

 

alert(System.scriptElement.outerHTML);
alert(System.path);

 

如当前js在html页面中的script标签<script src="./script/aa.js"></script>

得到的:

System.scriptElement = Object

System.scriptElement.outerHTML = <script src="./script/aa.js"></script>

System.scriptElement.src = ./script/aa.js

System.path = ./script

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值