javascript動的にscriptとlinkタグを生成し、ロードが完了したかどうかを確認します.
1840 ワード
動的にスクリプトタグを作成
function loadJs(script_file_url, script_id){
var head = document.head || document.getElementByTagName("head")[0] || document.documentElement;
var script = document.createElement("script");
script.setAttribute("type","text/javascript");
script.setAttribute("src",script_file_url);
script.setAttribute("id",script_id);
script.setAttribute("charset","utf-8");
var scriptId = document.getElementById(script_id);
// , 。
if(scriptId){
head.removeChild(scriptId);
}
head.appendChild(script);
if(window.all){
script.onreadystatechange = function(){
if(script.readyStatus === "loaded" || script.readyState === "complete"){
//
}
}
}
else{
script.onload = function(){
// 。
}
}
}
// test
var scriptUrl = "
var scriptId = "exampleId";
loadJs(scriptUrl,scriptId);
動的にlinkタグを作成します.function loadCss(css_file_url, link_id){
var head = document.head || document.getElementByTagName("head")[0] || document.documentElement;
var script = document.createElement("link");
script.setAttribute("rel","stylesheet");
script.setAttribute("href",css_file_url);
script.setAttribute("id",link_id);
script.setAttribute("charset","utf-8");
linkId = document.getElementById(link_id);
// , 。
if(linkId){
head.removeChild(linkId);
}
head.appendChild(linkId);
}
// test
var linkUrl = "http://example.com/asserts/test.css";
var linkId = "exampleId";
loadJs(linkUrl,linkId);