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

时间:2022-12-24 16:01:31

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?

获得绝对路径的最快方法是什么?

7 个解决方案

#1


40  

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.

对于返回的脚本数组中的每个元素,您可以访问其src属性。

The currently executing include file will always be the last one in the scripts array. 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.

当然,这只会在初始代码运行时起作用,并且在加载初始脚本后调用的函数中没有用处,因此如果您需要稍后可用的值,则需要将其保存到变量中。

#2


22  

Get the current pathname of the javascript file

获取javascript文件的当前路径名

Put this in your apache directory underneath /tmp and call it test.html. Visit the url

将它放在/ tmp下面的apache目录中,并将其命名为test.html。访问网址

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

Javascript:

<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>
  $(location).attr('href');      // http://localhost/tmp/test.html?blah=2#foobar
  $(location).attr('pathname');  // /tmp/test.html
</script>
</html>

#3


1  

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;
}

#4


0  

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

#5


0  

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.

希望它能为您节省大量时间。

#6


0  

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)

#7


-2  

Try this if you want to get the current file name or directory

如果要获取当前文件名或目录,请尝试此操作

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

#1


40  

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.

对于返回的脚本数组中的每个元素,您可以访问其src属性。

The currently executing include file will always be the last one in the scripts array. 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.

当然,这只会在初始代码运行时起作用,并且在加载初始脚本后调用的函数中没有用处,因此如果您需要稍后可用的值,则需要将其保存到变量中。

#2


22  

Get the current pathname of the javascript file

获取javascript文件的当前路径名

Put this in your apache directory underneath /tmp and call it test.html. Visit the url

将它放在/ tmp下面的apache目录中,并将其命名为test.html。访问网址

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

Javascript:

<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>
  $(location).attr('href');      // http://localhost/tmp/test.html?blah=2#foobar
  $(location).attr('pathname');  // /tmp/test.html
</script>
</html>

#3


1  

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;
}

#4


0  

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

#5


0  

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.

希望它能为您节省大量时间。

#6


0  

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)

#7


-2  

Try this if you want to get the current file name or directory

如果要获取当前文件名或目录,请尝试此操作

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