Search code examples
javascriptgetelementsbytagname

get scripts by tag name under IE 8


When I try to get for example all images using

getElementsByTagName('img');

there is no problems, but when I try to get all scripts

getElementsByTagName('script'); 

the function returns empty value. This problem exists only under IE8.

Any suggestions?


Solution

  • I checked it and this is working fine in all browser.

    getElementsByTagName('script') return an object. For chrome it returns as NodeList[], whereas in IE8 it returns as [object HTMLCollection].

    I'm not sure whether you are trying to access the src of the script used in your project. If so, try the below code

    window.onload=function() {
      var scripts = document.getElementsByTagName("script");
      for (var i=0;i<scripts.length;i++) {
         if (scripts[i].src) console.log(i,scripts[i].src)
         else console.log(i,scripts[i].innerHTML)
      }
    }
    

    Reference.