您的位置:首页 > Web前端 > JavaScript

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

2008-06-06 15:20 447 查看
方法一:

/**
* 全局函数
* 得到给定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文件
* 20080506 yuanjq
*/
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: