來源: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文件的绝对路径或相对路径的两法相关推荐

  1. js文件中发送ajax请求,ulr路径不起作用的解决办法

    前言: 在写Python项目的时候,单独的js文件发送ajax请求,并不起效果并且提示路径错误 错误原因分析: {% url 'myadmin_updategoodsgrade' %} # 是模板中的 ...

  2. thymeleaf 引如js文件后 函数不触发

    今天在springboot+thymeleaf项目中,用thymeleaf模板引入js文件. <script src="webjars/jquery/3.3.1-1/jquery.js ...

  3. js相对路径相关(比如:js中的路径依赖导入该js文件的路径)

    问题描述: 前几天调用同事的js接口文件,在他自己的html测试页面ok,在我这边调用时出现问题. debug过程中,将该测试html移到其他位置都不行,放到原html测试页面同层次路径下是OK的. ...

  4. 在js、jsp文件中如何获取项目绝对路径

    js.jsp如何文件获取项目绝对路径 1,js文件中获取项目绝对路径 2,jsp文件中获取项目绝对路径 原创地址:https://blog.csdn.net/QQ826688096/article/d ...

  5. Three.js加载.obj和.mtl文件(无法加载材质、路径错误问题)

    加载.obj模型文件 本文是Three.js电子书的14.3节 使用三维软件导出.obj模型文件的时候,会同时导出一个材质文件.mtl, .obj和.stl文件包含的信息一样都是几何体顶点相关数据,材 ...

  6. html img调用js,html调用js变量 如何在html中输出js文件中的变量

    html页面代码中怎么调用js变量?html页面代码中怎么调用js变量,例如 在html代码中插入js代码: a=取浏览你把index1.js 中的onReady 去掉,把index1.js改成 fu ...

  7. 只需三分钟!只需创建一个vuex.js文件,让你马上学会使用Vuex,尽管Vuex是个鸡肋!(扔掉store文件夹和里面的index、getters、actions、mutations等js文件吧!)

    前情提示:有一天,我要实现一个效果→点击某个按钮改变一个全局变量,并且要让绑定了该变量的所有位置异步渲染.我试过用一个全局的js文件存放该变量,该变量值虽然改变了,但是没有做到异步渲染.接着我用win ...

  8. Angular7中引用外部JS文件

    Angular7中引用外部JS文件,步骤如下: 1. 将引入的js文件放到项目的src/assets下 2. 在angular.json文件中找到scripts项并配置js文件的相对路径 3. 在sr ...

  9. vue引用js文件的多种方式

    本文主要介绍了vue引用js文件的多种方式,本文大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下 1.vue-cli webpack全局引入jquery (1) 首先 npm inst ...

最新文章

  1. 行为型模式:命令模式
  2. socket recv 服务端阻塞 python_网络编程(基于socket编程)
  3. 训练网络loss出现Nan的原因总结记录
  4. VS2008 安装盘的问题
  5. js php调用webservice,js跨域调用WebService的使用方法
  6. .NET MVC异步调用中的Session问题
  7. lua正则替换_lua 字符串 正则表达式 转义 特殊字符
  8. SQL Server 2016 新功能之综述
  9. 十七 Ajax校验用户名功能
  10. 华为设备如何将接口配置为中继模式_(完整版)华为设备基本配置命令
  11. 16.1 调色板的使用
  12. apk倒入android studio,反编译apk导入android studio
  13. PX4模块设计之九:PX4飞行模式简介
  14. Excel中输入整数却总是显示小数,如何调整?
  15. 如何解决仓库无线网络覆盖问题?
  16. 基于神经网络识别抑郁症附matlab代码
  17. 织梦多城市分站站群安装教程【二级域名版本】
  18. 巴菲特致股东的一封信:2005年
  19. sgd kmeans
  20. 矿大计算机考研上岸分数,这些考研院校,今年上岸更容易!初试占比高达70%!...

热门文章

  1. Qt Creator基本使用方法
  2. 最小二乘法C#实现,简单代码
  3. 一种新的图像清晰度评价函数,数字图像清晰度评价函数的研究与改进
  4. 在OpenCV中将cv::Mat绘制到MFC的视图中
  5. C#GDI画圆及填充
  6. C++(1) 指针 new 和delete
  7. php承载,javascript,php_如何防止无限循环的php CPU承载过重?,javascript,php - phpStudy...
  8. python 垃圾回收哪时候执行_Python 中的垃圾回收机制是如何工作的?
  9. 虚拟计算机组成,计算机组成原理虚拟仿真实验平台研究
  10. 诺奖经济学家:中国与世界可找到差异化空间推动合作共赢