我在文件中包含myscript.js,http://site1.com/index.html如下所示:
<script src=http://site2.com/myscript.js></script>
Run Code Online (Sandbox Code Playgroud)
在"myscript.js"中,我希望能够访问URL" http://site2.com/myscript.js ".我想要这样的东西:
function getScriptURL() {
// something here
return s
}
alert(getScriptURL());
Run Code Online (Sandbox Code Playgroud)
如果从上面提到的index.html调用,它会提醒" http://site2.com/myscript.js ".
lam*_*cck 53
来自http://feather.elektrum.org/book/src.html:
var scripts = document.getElementsByTagName('script');
var index = scripts.length - 1;
var myScript = scripts[index];
Run Code Online (Sandbox Code Playgroud)
该变量myScript现在具有脚本dom元素.您可以使用获取src网址myScript.src.
请注意,这需要作为脚本初始评估的一部分执行.如果您不想污染Javascript命名空间,您可以执行以下操作:
var getScriptURL = (function() {
var scripts = document.getElementsByTagName('script');
var index = scripts.length - 1;
var myScript = scripts[index];
return function() { return myScript.src; };
})();
Run Code Online (Sandbox Code Playgroud)
tsd*_*sds 48
您可以将id属性添加到脚本标记中(即使它位于head标记内):
<script id="myscripttag" src="http://site2.com/myscript.js"></script>
Run Code Online (Sandbox Code Playgroud)
然后按如下方式访问其src:
document.getElementById("myscripttag").src
Run Code Online (Sandbox Code Playgroud)
当然,对于包含您脚本的每个文档,id值应该相同,但我认为这对您来说不是很大的不便.
3DR*_*ert 24
使用DOM和querySelector,您可以获取特定脚本:
var dir = document.querySelector('script[src$="myscript.js"]').getAttribute('src');
var name = dir.split('/').pop();
dir = dir.replace('/'+name,"");
Run Code Online (Sandbox Code Playgroud)
dav*_*010 22
IE以外的所有东西都支持
document.currentScript
Run Code Online (Sandbox Code Playgroud)
Que*_*mer 18
我写了一个类来查找使用延迟加载和异步脚本标记的脚本路径.
我有一些与我的脚本相关的模板文件,因此我创建的类不是硬编码,而是自动创建路径.完整的源代码在github上.
前段时间我曾使用arguments.callee尝试做类似的事情,但我最近在MDN上看到它在严格模式下是不允许的.
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('.');
};
}
Run Code Online (Sandbox Code Playgroud)
Mr.*_*kin 14
如果你有机会使用jQuery,代码将如下所示:
$('script[src$="/myscript.js"]').attr('src');
Run Code Online (Sandbox Code Playgroud)
简单而直接的解决方案效果很好:
如果不是IE,则可以使用document.currentScript
IE document.querySelector('script[src*="myscript.js"]')
:
function getScriptURL(){
var script = document.currentScript || document.querySelector('script[src*="myscript.js"]')
return script.src
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
60697 次 |
| 最近记录: |