Javascript 如何从自己的js文件中找到脚本路径

Javascript 如何从自己的js文件中找到脚本路径,javascript,jquery,Javascript,Jquery,我想在自己的js文件中找到脚本路径 所以我想要一个字符串“C:\files\MyProject\MyScripts\MyJavaScript.js” 这怎么可能呢?客户端您无法访问物理路径。不过,您可以获得script标记的src属性 服务器端可以获取物理路径。例如(C#): 要在JavaScript中查找URL的完整路径,请使用此location.href如果您可以将服务器的绝对路径定义为常量变量 您可以通过从window.location.href强制转换来完成此操作。从window.loc

我想在自己的js文件中找到脚本路径

所以我想要一个字符串“C:\files\MyProject\MyScripts\MyJavaScript.js”


这怎么可能呢?

客户端您无法访问物理路径。不过,您可以获得
script
标记的
src
属性

服务器端可以获取物理路径。例如(C#):


要在JavaScript中查找URL的完整路径,请使用此location.href

如果您可以将服务器的绝对路径定义为常量变量

您可以通过从
window.location.href
强制转换来完成此操作。从
window.location.href
中删除主机前缀
window.location.host
,并使用服务器的绝对路径进行前置

尝试:


试试这个解决方案。我想这正是你想要的:)

将此代码放入每个链接的脚本文件中

var scriptEls = document.getElementsByTagName( 'script' );
var thisScriptEl = scriptEls[scriptEls.length - 1];
var scriptPath = thisScriptEl.src;
var scriptFolder = scriptPath.substr(0, scriptPath.lastIndexOf( '/' )+1 );

console.log(scriptPath +"  "+ scriptFolder );// you can save these in any variable also
我使用以下HTML代码对其进行了测试:

<!DOCTYPE html>
<html>
  <head> 
      <title>testing...</title>  
      <script type="text/javascript" src="test.js"></script> 
      <script type="text/javascript" src="js/test2.js"></script> 
      <script type="text/javascript" src="../test3.js"></script> 
    </head>  
    <body>
     content area
    </body>
</html>
特别感谢。。 希望这有帮助。

您可以尝试(Jquery):


myScriptDetails将包含有关脚本的详细信息,包括其位置

以下是我是如何做到的:

function ScriptPath() {
  var scriptPath = '';
  try {
    //Throw an error to generate a stack trace
    throw new Error();
  }
  catch(e) {
    //Split the stack trace into each line
    var stackLines = e.stack.split('\n');
    var callerIndex = 0;
    //Now walk though each line until we find a path reference
    for(var i in stackLines){
      if(!stackLines[i].match(/http[s]?:\/\//)) continue;
      //We skipped all the lines with out an http so we now have a script reference
      //This one is the class constructor, the next is the getScriptPath() call
      //The one after that is the user code requesting the path info (so offset by 2)
      callerIndex = Number(i) + 2;
      break;
    }
    //Now parse the string for each section we want to return
    pathParts = stackLines[callerIndex].match(/((http[s]?:\/\/.+\/)([^\/]+\.js)):/);
  }

  this.fullPath = function() {
    return pathParts[1];
  };

  this.path = function() {
    return pathParts[2];
  };

  this.file = function() {
    return pathParts[3];
  };

  this.fileNoExt = function() {
    var parts = this.file().split('.');
    parts.length = parts.length != 1 ? parts.length - 1 : 1;
    return parts.join('.');
  };
}

完全不清楚…为什么需要脚本路径可能重复?如果您只想知道js文件在哪里实现,可以搜索此文件。myScriptDetails将包含页面上链接的所有脚本的详细信息。这里的问题是脚本文件如何找到自己的位置。
<!DOCTYPE html>
<html>
  <head> 
      <title>testing...</title>  
      <script type="text/javascript" src="test.js"></script> 
      <script type="text/javascript" src="js/test2.js"></script> 
      <script type="text/javascript" src="../test3.js"></script> 
    </head>  
    <body>
     content area
    </body>
</html>
file:///D:/workspace/dbshell/www/test.js  file:///D:/workspace/dbshell/www/          test.js:6
file:///D:/workspace/dbshell/www/js/test2.js  file:///D:/workspace/dbshell/www/js/   test2.js:6
file:///D:/workspace/dbshell/test3.js  file:///D:/workspace/dbshell/                 test3.js:6
var myScriptDetails = $('script');
function ScriptPath() {
  var scriptPath = '';
  try {
    //Throw an error to generate a stack trace
    throw new Error();
  }
  catch(e) {
    //Split the stack trace into each line
    var stackLines = e.stack.split('\n');
    var callerIndex = 0;
    //Now walk though each line until we find a path reference
    for(var i in stackLines){
      if(!stackLines[i].match(/http[s]?:\/\//)) continue;
      //We skipped all the lines with out an http so we now have a script reference
      //This one is the class constructor, the next is the getScriptPath() call
      //The one after that is the user code requesting the path info (so offset by 2)
      callerIndex = Number(i) + 2;
      break;
    }
    //Now parse the string for each section we want to return
    pathParts = stackLines[callerIndex].match(/((http[s]?:\/\/.+\/)([^\/]+\.js)):/);
  }

  this.fullPath = function() {
    return pathParts[1];
  };

  this.path = function() {
    return pathParts[2];
  };

  this.file = function() {
    return pathParts[3];
  };

  this.fileNoExt = function() {
    var parts = this.file().split('.');
    parts.length = parts.length != 1 ? parts.length - 1 : 1;
    return parts.join('.');
  };
}