以下 JavaScript 代码的主要用途是什么?
<script>
var QunarUtil=new function(){var prefix='/scripts/';var suffix='';var host='';if(location.host.indexOf('src.')!=-1){prefix='/scripts/src/';host='http://hstatic.qunar.com';suffix='.js';}else if(location.host.indexOf('enc.')!=-1){prefix='/scripts/';host='http://hstatic.qunar.com';suffix='.js';}else if(location.host.substring(0,10)=='sdev-'){prefix=location.host.substring(5,location.host.indexOf('.'));prefix='/'+prefix.replace(/\-/ig,'/');host='http://hstatic.qunar.com';suffix='.js';}else if(location.host.indexOf("h.qunar.com")!=-1){host='http://hstatic.qunar.com';suffix='';}
this.getScriptURL=function(name,isList){if(name.charAt(0)!='/')
return this.getScript(prefix+name,isList);else
return this.getScript(name,isList);}
this.getScript=function(src,isList){return'<'+'script type="text/javascript" src="'+host+src+(isList?suffix:'.js')+'?'+__QUNARVER__+'"></'+'script>';}
this.writeScript=function(name,isList){document.write(this.getScriptURL(name,isList));}
this.writeScriptList=function(list){for(var i=0;i<list.length;i++)
document.write(this.getScriptURL(list[i]));}
var cssRoot='/styles/';this.writeCSS=function(cssList){for(var i=0;i<cssList.length;i++){document.write('<link rel="stylesheet" href="'+cssRoot+cssList[i]+'?'+__QUNARVER__+'">');}}
this.writeStaticScript=function(src){document.write('<scr'+'ipt type="text/javascript" src="'+src+'"></'+'scr'+'ipt>');}
this.writeStaticList=function(src){document.write('<scr'+'ipt type="text/javascript" src="'+src+suffix+'?'+__QUNARVER__+'"></'+'scr'+'ipt>');}}
$include=function(){for(var i=0;i<arguments.length;i++){QunarUtil.writeScript(arguments[i],true);}}
</script>
<小时/>
未压缩版本:
<script>
var QunarUtil = new
function() {
var prefix = '/scripts/';
var suffix = '';
var host = '';
if (location.host.indexOf('src.') != -1) {
prefix = '/scripts/src/';
host = 'http://hstatic.qunar.com';
suffix = '.js';
} else if (location.host.indexOf('enc.') != -1) {
prefix = '/scripts/';
host = 'http://hstatic.qunar.com';
suffix = '.js';
} else if (location.host.substring(0, 10) == 'sdev-') {
prefix = location.host.substring(5, location.host.indexOf('.'));
prefix = '/' + prefix.replace(/\-/ig, '/');
host = 'http://hstatic.qunar.com';
suffix = '.js';
} else if (location.host.indexOf("h.qunar.com") != -1) {
host = 'http://hstatic.qunar.com';
suffix = '';
}
this.getScriptURL = function(name, isList) {
if (name.charAt(0) != '/') return this.getScript(prefix + name, isList);
else
return this.getScript(name, isList);
}
this.getScript = function(src, isList) {
return '<' + 'script type="text/javascript" src="' + host + src + (isList ? suffix : '.js') + '?' + __QUNARVER__ + '"></' + 'script>';
}
this.writeScript = function(name, isList) {
document.write(this.getScriptURL(name, isList));
}
this.writeScriptList = function(list) {
for (var i = 0; i < list.length; i++)
document.write(this.getScriptURL(list[i]));
}
var cssRoot = '/styles/';
this.writeCSS = function(cssList) {
for (var i = 0; i < cssList.length; i++) {
document.write('<link rel="stylesheet" href="' + cssRoot + cssList[i] + '?' + __QUNARVER__ + '">');
}
}
this.writeStaticScript = function(src) {
document.write('<scr' + 'ipt type="text/javascript" src="' + src + '"></' + 'scr' + 'ipt>');
}
this.writeStaticList = function(src) {
document.write('<scr' + 'ipt type="text/javascript" src="' + src + suffix + '?' + __QUNARVER__ + '"></' + 'scr' + 'ipt>');
}
}
$include = function() {
for (var i = 0; i < arguments.length; i++) {
QunarUtil.writeScript(arguments[i], true);
}
}
</script>
最佳答案
它是一个从 http://hstatic.qunar.com 加载 JavaScript 和 CSS 文件的实用程序.
按原样,该代码将无法工作,因为它依赖于一个名为 __QUARVER__
下面是带有内联文档的代码版本。
//Expose the variable QunarUtil into the global namespace, which is an instance of an anonymous function.
var QunarUtil = new function() {
//set up internal variable in QunarUtil
var prefix = '/scripts/';
var suffix = '';
var host = '';
//Modify prefix, host & suffix if the current host (think webpage server
//address in the browser) has 'src.' as part of it's hostname.
if (location.host.indexOf('src.') != -1) {
prefix = '/scripts/src/';
host = 'http://hstatic.qunar.com';
suffix = '.js';
//Modify prefix, host & suffix if the current host has 'enc.' as part of it's hostname.
} else if (location.host.indexOf('enc.') != -1) {
prefix = '/scripts/';
host = 'http://hstatic.qunar.com';
suffix = '.js';
//Modify prefix, host & suffix if the current host is 'sdev-' (and nothing
//else since it is comparing a 5 char string with 10 char substring).
} else if (location.host.substring(0, 10) == 'sdev-') {
prefix = location.host.substring(5, location.host.indexOf('.'));
prefix = '/' + prefix.replace(/\-/ig, '/');
host = 'http://hstatic.qunar.com';
suffix = '.js';
//Modify prefix, host & suffix if the current host has 'h.qunar.com' as part of it's hostname.
} else if (location.host.indexOf("h.qunar.com") != -1) {
host = 'http://hstatic.qunar.com';
suffix = '';
}
//Expose a method on the QunarUtil variable called getScriptURL.
this.getScriptURL = function(name, isList) {
if (name.charAt(0) != '/')
return this.getScript(prefix + name, isList);
else
return this.getScript(name, isList);
}
//Expose a method on the QunarUtil variable called getScript.
this.getScript = function(src, isList) {
return '<' + 'script type="text/javascript" src="' + host +
src + (isList ? suffix : '.js') + '?' + __QUNARVER__ + '"></' + 'script>';
}
//Expose a method on the QunarUtil variable called writeScript.
this.writeScript = function(name, isList) {
document.write(this.getScriptURL(name, isList));
}
//Expose a method on the QunarUtil variable called writeScriptList.
this.writeScriptList = function(list) {
for (var i = 0; i < list.length; i++)
document.write(this.getScriptURL(list[i]));
}
//Set a new internal QunarUtil variable.
var cssRoot = '/styles/';
//Expose a method on the QunarUtil variable called writeCSS.
this.writeCSS = function(cssList) {
for (var i = 0; i < cssList.length; i++) {
document.write('<link rel="stylesheet" href="' + cssRoot + cssList[i] + '?' + __QUNARVER__ + '">');
}
}
//Expose a method on the QunarUtil variable called writeStaticScript.
this.writeStaticScript = function(src) {
document.write('<scr' + 'ipt type="text/javascript" src="' + src + '"></' + 'scr' + 'ipt>');
}
//Expose a method on the QunarUtil variable called writeStaticList.
this.writeStaticList = function(src) {
document.write('<scr' + 'ipt type="text/javascript" src="' + src + suffix +
'?' + __QUNARVER__ + '"></' + 'scr' + 'ipt>');
}
}
//Expose a global variable called $inclue which take an arbitrary number
//of JavaScript-file URLs as string arguments and loops over them to
//create a bunch of <script> tags using the QunarUtil.writeScript method.
$include = function() {
for (var i = 0; i < arguments.length; i++) {
QunarUtil.writeScript(arguments[i], true);
}
}
此代码与您喜欢的 float Google map 无关,但它可能用于加载相关代码。
关于javascript - JScript 有帮助吗?这段 JavaScript 代码的主要目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4704463/