开发者

Including JavaScript at runtime is not working in IE8

开发者 https://www.devze.com 2023-01-27 12:11 出处:网络
The below piece of code was working in IE6 & IE7 and almost all versions of FF. It just don\'t work in IE8. It doesn\'t work in the sense once I added the script tag in to HTML->HEAD element I don

The below piece of code was working in IE6 & IE7 and almost all versions of FF. It just don't work in IE8. It doesn't work in the sense once I added the script tag in to HTML->HEAD element I don't see the script being loaded in the browser(the alerts in the s开发者_开发知识库cript doesn't show up). I see the tags have been inserted in the HTML-HEAD though.

var head = document getElementsByTagName('head')[0];
// Check if the script is already loaded.
if (head ){
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.language = 'JavaScript';
    script.src = '/Tolven/scripts/' + jsFileName;
    head.appendChild(script);       
}

Does anybody have this issue? Or any clues to resolve this?


If this script is in <head> tag than head does not exists when this script is parsed and executed. So, of cource if (head) is false.

Your are using JS framework -- so feel free to use it's tools. And also do not forget to include Your framework, before using it.

<!-- if your are using mootools -->
<script type="text/javascript" src="mootools.js"></script>
<script type="text/javascript">
  window.addEvent('domready', function() {
    // Your code...
  });
</script>

<!-- if your are using prototype -->
<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript">
  document.observe("dom:loaded", function() {
    // Your code...
  });
</script>


Consider using a library like RequireJS or LABjs that do the job of including scripts at runtime really well.


var head = document getElementsByTagName('head')[0];

should be

var head = document.getElementsByTagName('head')[0];

Script seems to work after this modification.


This is was actually working. There is was an error(it only happens in IE8) in one of the scripts that's inserted at runtime. Eventually it's not executing alerts in the pages loaded next. Thanks for your answers though.

0

精彩评论

暂无评论...
验证码 换一张
取 消