我很难理解你真正想要的是什么,但你可以遍历所有脚本并解析src
.
jQuery:
function getHosts(scriptName){
var hosts = [];
scriptName = scriptName.replace(/([.])/g,"\\$1"); // escape special characters
$('script').each(function(){
var _src = this.src;
if ( RegExp(scriptName).test(_src) ){
_src = _src.replace(/^https*:\/\//,''); // remove protocol
_src = _src.replace(/\/.*$/,''); // remove rest of path
hosts[hosts.length] = _src; // store it for return
}
});
return hosts; // return results as array
}
alert( getHosts('script.js').join(',') );
并且没有 jQuery:
function getHosts(scriptName){
var hosts = [];
scriptName = scriptName.replace(/([.])/g,"\\$1"); // escape special characters
var scripts = document.getElementsByTagName('script');
for (var n=scripts.length;n--;){
var _src = scripts[n].src;
if ( RegExp(scriptName).test(_src) ){
_src = _src.replace(/^https*:\/\//,''); // remove protocol
_src = _src.replace(/\/.*$/,''); // remove rest of path
hosts[hosts.length] = _src; // store it for return
}
}
return hosts; // return results as array
}
alert( getHosts('test.js').join('\n') );