得到Js文件的绝对路径或相对路径的两法
2008-06-06 15:20
519 查看
方法一:
/**
* 全局函数
* 得到给定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
/**
* 全局函数
* 得到给定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
相关文章推荐
- 得到Js文件的绝对路径或相对路径的两法
- 调用js文件时的相对路径和绝对路径
- [JS] - 火狐得到文件的绝对路径(暂时的方法)
- js获取fileupload文件绝对路径.在vs2008下调试运行和建站运行得到的文件路径不一致
- 文件流_File文件操作_相对路径_绝对路径
- 在类中取得当前文件所在的相对路径与绝对路径
- 用MFC实现代码获取当前EXE 执行的绝对路径,用于扩展相对路径扩展,以及计算文件大小功能
- 【转】得到两个文件的相对路径 [php]
- mkdir()与 mkdirs()方法的区别 ;目录和文件的区别;绝对路径和相对路径的详解;file的getPath getAbsolutePath和getCanonicalPath的不同
- Spring MVC程序中得到静态资源文件css,js,图片文件的路径问题总结
- Java中关于文件的绝对路径与相对路径的总结(转)
- JavaWeb(EE)相对路径_绝对路径详解与得到相关路径
- 相对与绝对路径,创建删除目录文件,CD与RM命令
- linux c编程,选用popen()得到一个相对路径的绝对路径
- 相对路径产生js文件、html文件。
- JS魔法堂:获取当前脚本文件的绝对路径
- 文件的相对路径和绝对路径
- jar包中读取文件系统绝对路径,相对路径,以及jar包内部路径的写法
- 在Servlet与JSP中取得当前文件所在的相对路径与绝对路径(转)
- 写文件时最好使用绝对路径, 而不使用相对路径