得到Js文件的绝对路径或相对路径的两法
來源:http://blog.csdn.net/yuansicau/archive/2008/06/06/2517082.aspx
?
方法一:
/**
?* 全局函數
?* 得到給定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文件的绝对路径或相对路径的两法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 对自己推文浏览量过低不满,消息称马斯克命
- 下一篇: 面向服务的分析与设计原理