要推迟Javascript,我使用以下代码。它有效。
<script type="text/javascript">
function downloadJSAtOnload() {
var element2 = document.createElement("script");
element.src = "http://s7.addthis.com/js/300/addthis_widget.js";
document.body.appendChild(element);
var element3 = document.createElement("script");
element.src = "js/jquery.idTabs.min.js";
document.body.appendChild(element);
var element4 = document.createElement("script");
element.src = "js/mobilemelody.jq.js";
document.body.appendChild(element);
var element5 = document.createElement("script");
element.src = "js/swipe.min.js";
document.body.appendChild(element);
var element6 = document.createElement("script");
element.src = "js/swipe.js";
document.body.appendChild(element);
var element7 = document.createElement("script");
element.src = "js/mobilemelody.js";
document.body.appendChild(element);
var element8 = document.createElement("script");
element.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js";
document.body.appendChild(element);
}
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload);
else window.onload = downloadJSAtOnload;
</script>
现在我开始使用cloudfront cdn,并从子域content.exemple.com为js文件提供服务。 子域content.exemple.com指向cloudfront cdn。 例子js / bootstrap.min.js已成为http://content.exemple.com/js/bootstrap.min.js 我使用了以下代码而没有任何结果:
<script type="text/javascript">
function downloadJSAtOnload() {
var element2 = document.createElement("script");
element.src = "//s7.addthis.com/js/300/addthis_widget.js#pubid=xa-515d67835fedfa10#";
document.body.appendChild(element);
var element3 = document.createElement("script");
element.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js";
document.body.appendChild(element);
var element4 = document.createElement("script");
element.src = "http://content.exemple.com/min/?f=templates/default/js/bootstrap.min.js,templates/default/js/jquery.cookee.js,templates/default/js/jquery.validate.min.js";
document.body.appendChild(element);
var element5 = document.createElement("script");
element.src = "http://exemple.avimeo.com/min/?f=templates/default/js/jquery.maskedinput-1.3.min.js,templates/default/js/jquery.tagsinput.min.js,templates/default/js/jquery-scrolltofixed-min.js,templates/default/js/jquery.uniform.min.js,templates/default/js/jquery.ba-dotimeout.min.js";
document.body.appendChild(element);
var element6 = document.createElement("script");
element.src = "js/swipe.js";
document.body.appendChild(element);
var element7 = document.createElement("script");
element.src = "http://content.exemple.com/js/jquery.typewatch.js";
document.body.appendChild(element);
var element8 = document.createElement("script");
element.src = "http://content.exemple.com/min/?f=templates/default/js/waypoints.min.js,templates/default/js/melody.social.min.js";
document.body.appendChild(element);
}
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload);
else window.onload = downloadJSAtOnload;
</script>