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

问题描述:

我有一个名为main.js的JavaScript文件。如何获取当前javascript文件名的绝对路径

里面main.js我想这个当前文件的绝对路径,像:

http://server/app/main.js 

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

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

您可以在调查收集脚本:

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

对于返回scripts阵列中的每个元素,你可以访问其src属性。

当前正在执行的包含文件将始终是scripts阵列中的最后一个。所以你可以在scripts[scripts.length-1]访问它。

当然这只会在初始代码运行的时候才起作用,例如在加载初始脚本后调用的函数中不会有用,所以如果您需要稍后可用的值,则需要保存它到一个变量。

+0

感谢,这正是我想要 – hguser

+0

如果我们已经包含多个文件,将它的工作...那么每个js文件如何显示它自己的src路径.. ?? – xxbinxx

+0

值得注意的是,您可能想要使用另一种方法从'scripts'数组中获取想要的脚本。我使用这种方法,但是有报道称,在Firefox中每隔一段时间(blegh ...)就会有另一个脚本在我的逻辑抓取该元素数组的时候加载。 聪明的事情就是像最后3个那样抓取并循环遍历它们,并找到具有要查找的文件名的文件。 –

var path = document.location.pathname 

将提供当前html页面。

如果你看看当前脚本,尝试在这个网站中提到的方法:

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

获取的JavaScript文件

在Apache目录将这个下的电流路径/ tmp并将其称为test.html。访问URL

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> 

的更多信息:​​

或者,如果您有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> 

这将工作。但是,它要求您已经知道脚本的文件名是什么。但在大多数情况下,你会知道这一点。

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

这是一个获取css文件绝对位置的函数。 https://gist.github.com/jamrizzi/44bac4240057b4a89a2d918d05680e3d – jamrizzi

,如果你想获得当前的文件名或目录

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

+0

location.href获取当前网页的url,而不是当前正在执行的javascript文件。 – CpnCrunch

+0

我们首先得到的网址,比分裂它得到所需的部分。 location.href.split('/')[0]; //获取第一部分 location.href.split('/')[location.href.split('/')。length - 1]; //获取文件名 – Amr

+0

不,这是不正确的。您正在获取页面的网址,而不是当前的JavaScript文件。如果您将该JavaScript代码放入www.def.com/page.html中的www.abc.com/file.js文件中,location.href将返回www.def.com/page.html。问题是如何获取JavaScript文件的名称,即www.abc.com/file.js。 – CpnCrunch

试试这个我知道这是一个老问题,但还是实际的,因为IE浏览器仍然没有提供脚本src。

最佳形式我是给script标签的唯一ID:

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

脚本我使用内部:

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

它将保持SRC格式,因为它是(相对或完成)并在IE和Chrome中进行测试。

希望它能为您节省很多时间。

很容易

将这个在* js文件,你想要得到的路径:

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