动态加载js&动态加载css

今天看一个游戏页面的代码,想研究一下他们的通调怎么做来着。看到了一段代码if判断写法挺怪异的。

if(document.getElementsByTagName("head")[0]||document.getElementsByTagName("body")[0],src="/global_site_inc/topBar3.js?100317";)
//也就是说if条件判断语句中可以对变量赋值if(条件,int a)

(function(){
var o = document.getElementsByTagName("head")[0]||document.getElementsByTagName("body")[0],src="/global_site_inc/topBar3.js?100317";
if(o){
var js = document.createElement("script");
js.setAttribute("defer","defer");
js.setAttribute("src",src);
js.setAttribute("type","text/javascript");
o.appendChild(js);
}
else document.write('<script type="text/javascript" defer="defer" src="'+src+'">'+'<\/scr'+'ipt>');
})();

下边是网上转载的一个对js,css 动态加载的写法。

/* Impot Common script*/
function addJS(jsfile)
{

var head = document.getElementsByTagName('HEAD').item(0);
var script = document.createElement('SCRIPT');
script.src = jsfile;
script.type = "text/javascript";
head.appendChild(script);
}

function addCSS(cssfile) {
var head = document.getElementsByTagName('HEAD').item(0);
var style = document.createElement('link');
style.href = cssfile;
style.rel = 'stylesheet'
style.type = 'text/css';
head.appendChild(style);
}

/* LoadScripts at here.*/

function LoadScripts()
{

addJS(_ResourcePath+"Scripts/ToolBar.js");
}

function LoadCSS()
{

addCSS(_ResourcePath+"Styles/default.css");
}

比较标准的例子,供参考:

function loadjscssfile(filename, filetype){
if (filetype=="js"){ //if filename is a external JavaScript file
var fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript")
fileref.setAttribute("src", filename)
}
else if (filetype=="css"){ //if filename is an external CSS file
var fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet")
fileref.setAttribute("type", "text/css")
fileref.setAttribute("href", filename)
}
if (typeof fileref!="undefined")
document.getElementsByTagName("head")[0].appendChild(fileref)
}

loadjscssfile("myscript.js", "js") //dynamically load and add this .js file
loadjscssfile("javascript.php", "js") //dynamically load "javascript.php" as a JavaScript file
loadjscssfile("mystyle.css", "css") //dynamically load and add this .css file