3

I am making add Script utility function for my project.it is working good in chrome,firefox,IE10,IE9 but it is throwing "Unknown run time error" in IE8

util.addScript = function appendStyle(scriptcontetnt) {
    var st = document.createElement('script');
    st.type = "text/javascript";


    st.innerHTML = scriptcontetnt; //throw error at this point

    document.getElementsByTagName('head')[0].appendChild(st);
    return true;
};

I know using innerhtml throw errror in ie8 so i read diferent threads related to this but did't get any correct solution for this issue

2 Answers2

0

innerHTML may not be the best solution for a script tag, try this:

util.addScript = function appendStyle(scriptcontetnt) {
    var st = document.createElement('script');
    st.type = "text/javascript";

    try {
      st.innerHTML = scriptcontetnt;     
    } catch(e) {
      // IE has funky script nodes
      st.text = scriptcontetnt;
    }

    document.getElementsByTagName('head')[0].appendChild(st);
    return true;
};
Emilio Rodriguez
  • 5,709
  • 4
  • 27
  • 32
0

According to this Create script tag in IE8 you should use text instead of innerHTML in IE < 9

function appendStyle(scriptcontetnt) {
   var st = document.createElement('script');
   st.type = "text/javascript";
   st.text = scriptcontetnt; //throw error at this point
   document.getElementsByTagName('head')[0].appendChild(st);
   return true;
}; 
Community
  • 1
  • 1
pdjota
  • 3,163
  • 2
  • 23
  • 33