1. 程式人生 > >動態腳本(兼容所有瀏覽器)

動態腳本(兼容所有瀏覽器)

text tee node script pen 示例 string app cti

function loadScriptString(code){

  var script = document.createElement("script");

  script.type = "text/javascript";

  try{

    script.appendChild(document.createTextNode(code));

  } catch(ex){

    script.text = code;

  }

  document.body.appendChild(script);

}

調用示例

loadScriptString("function sayhi(){alert(‘hi‘);}")

動態腳本(兼容所有瀏覽器)