简体   繁体   English

如何获取当前javascript文件名的绝对路径

[英]How to get the absolute path of the current javascript file name

I have a javascript file named main.js .我有一个名为main.js的 javascript 文件。

Inside main.js I want to get the absolute path of this current file, something like:main.js ,我想获取当前文件的绝对路径,例如:

http://server/app/main.js

http://server/app/script/main.js

What is the fastest way to get the absolute path?获得绝对路径的最快方法是什么?

You can investigate script collection at:您可以在以下位置调查脚本收集:

var scripts = document.getElementsByTagName("script");

For each element in the returned scripts array you can access its src attribute.对于返回的scripts数组中的每个元素,您可以访问其src属性。

The currently executing include file will always be the last one in the scripts array.当前执行的包含文件将始终是scripts数组中的最后一个。 So you can access it at scripts[scripts.length-1] .所以你可以在scripts[scripts.length-1]访问它。

Of course this will only work at time of initial code run and would not be useful for example within a function that is called after initial script is loaded, so if you need the value available later, you would need to save it to a variable.当然,这只会在初始代码运行时起作用,例如在加载初始脚本后调用的函数中没有用处,因此如果您需要稍后可用的值,则需要将其保存到变量中。

Get the current pathname of the javascript file获取 javascript 文件的当前路径名

Put this in your apache directory underneath /tmp and call it test.html.将其放在 /tmp 下的 apache 目录中,并将其命名为 test.html。 Visit the url访问网址

localhost/grader/test.html?blah=2#foobar

Javascript: Java脚本:

<html>
<script>
  alert(location.pathname);  // /tmp/test.html
  alert(location.hostname);  // localhost
  alert(location.search);    // ?blah=2
  alert(document.URL);       // http://localhost/tmp/test.html?blah=2#foobar
  alert(location.href);      // http://localhost/tmp/test.html?blah=2#foobar
  alert(location.protocol);  // http:
  alert(location.host);      // localhost
  alert(location.origin);    // http://localhost
  alert(location.hash);      // #foobar
</script>                            
</html>

More information on attributes of location: http://www.w3schools.com/jsref/obj_location.asp有关位置属性的更多信息:http: //www.w3schools.com/jsref/obj_location.asp

Or if you have jquery:或者如果你有 jquery:

<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js">
</script>
<script>
  $(location).attr('href');      // http://localhost/tmp/test.html?blah=2#foobar
  $(location).attr('pathname');  // /tmp/test.html
</script>
</html>

This will work.这会起作用。 However, it requires that you already know what the filename of the script is.但是,它要求您已经知道脚本的文件名是什么。 But in most situations you would know that.但在大多数情况下,你会知道这一点。

function absFileLoc(filename) {
  var scriptElements = document.getElementsByTagName('script');
  for (var i = 0; i < scriptElements.length; i++) {
    var source = scriptElements[i].src;
    if (source.indexOf(filename) > -1) {
      var location = source.substring(0, source.indexOf(filename)) + filename;
      return location;
    }
  }
  return false;
}

Modern way现代方式

The fastest way to get the absolute path is to use document.currentScript获取绝对路径的最快方法是使用document.currentScript

const url = document.currentScript.src;

Also to read components of a url you can use URL :)还可以使用URL来读取 url 的组件 :)

const url = new URL('http://www.example.com/cats');
console.log(url.hostname); // "www.example.com"
console.log(url.pathname); // "/cats"

All modern browsers should support these methods.所有现代浏览器都应该支持这些方法。

var path = document.location.pathname

will provide the current html page.将提供当前的 html 页面。

if you look for the current script, try the way mentioned in this site:如果您查找当前脚本,请尝试本站提到的方式:

http://bencarpenter.co.uk/javascript-path-to-the-current-script http://bencarpenter.co.uk/javascript-path-to-the-current-script

I know this is an older question, but still actual because IE still not provides the script src.我知道这是一个较旧的问题,但仍然是实际的,因为 IE 仍然没有提供脚本 src。

The best form me was to give the script tag an unique id:最好的形式是给脚本标签一个唯一的 id:

    <script id="kuvaScript" src="jscripts/kuva/kuva.min.js"></script>

Inside the script i use:在我使用的脚本中:

    var thisScript = $("#kuvaScript").attr("src");
    var thisPath = thisScript.split('/').slice(0, -1).join('/')+'/'; 

It will keep the src format as it is (relative or complete) and was tested in IE and Chrome.它将保持原样的 src 格式(相对或完整),并在 IE 和 Chrome 中进行了测试。

Hope it will save you a lot of time.希望它能为你节省很多时间。

Very easy非常简单

Put this in the *.js file you want to get path:把它放在你想要获取路径的 *.js 文件中:

let scripts = document.getElementsByTagName('script')
let lastScript = scripts[scripts.length -1]
console.log('lastScript', lastScript.src)

Try this if you want to get the current file name or directory如果你想获取当前文件名或目录,试试这个

 location.href.split('/')[ location.href.split('/').length - 1 ];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM