动态加载外部 css和javascript脚本 文件
2009-04-11 21:20
701 查看
在Google到的,随便收藏一个CSS模板站点(http://www.freecsstemplates.org/)
The problem in question is how to find out a dynamically included JavaScript file is actually loaded. The concept of JavaScript includes is here, the IE-only solution is here. The IE solution is to use the
CSS inclusion - IE fires both events,
JS inclusion - IE fires
2. In IE you have two ways to tell when a CSS is included.
The problem in question is how to find out a dynamically included JavaScript file is actually loaded. The concept of JavaScript includes is here, the IE-only solution is here. The IE solution is to use the
onreadystatechangeevent that is fired when a new scriptis included. It also works for dynamically loaded CSS files using a new
linkDOM element. Thanks to the comment from Björn Graf, I tried using
onloadevent to test if the new scriptis included using Firefox. It worked!
The code
What we have here (demo) is trying to include a .js file and an .css file, creating newscriptand
linkDOM elements. Then I'm attaching event listeners to those new elements - one
onloadand one
onreadystatechange. The scriptthat is included (jsalert.js) has one
alert().
var css; function include_css(css_file) { var html_doc = document.getElementsByTagName('head')[0]; css = document.createElement('link'); css.setAttribute('rel', 'stylesheet'); css.setAttribute('type', 'text/css'); css.setAttribute('href', css_file); html_doc.appendChild(css); // alert state change css.onreadystatechange = function () { if (css.readyState == 'complete') { alert('CSS onreadystatechange fired'); } } css.onload= function () { alert('CSS onloadfired'); } return false; } var js; function include_js(file) { var html_doc = document.getElementsByTagName('head')[0]; js = document.createElement('script'); js.setAttribute('type', 'text/javascript'); js.setAttribute('src', file); html_doc.appendChild(js); js.onreadystatechange = function () { if (js.readyState == 'complete') { alert('JS onreadystate fired'); } } js.onload= function () { alert('JS onloadfired'); } return false; }
Results
As you can probably guess, the results are different in IE and FF.CSS inclusion - IE fires both events,
onloadfirst, then
onreadystatechange. FF fires nothing.
JS inclusion - IE fires
onreadystatechange. FF fires
onload. Both will execute the scriptbefore firing the event.
Conclusion
1. So there is, after all, a cross-browser way to tell when a JavaScript is actually included and that is to attach two event listeners -onloadand
onreadystatechange.
2. In IE you have two ways to tell when a CSS is included.
相关文章推荐
- 使用javascript动态加载外部css或js文件
- 如何实现JavaScript动态加载CSS和JS文件
- 动态加载外部CSS与JS文件
- JavaScript动态加载css和js文件
- javascript/js 原生动态引入外部css文件及动态插入css代码片段
- 动态引用外部的Javascript脚本文件
- 动态载入/删除/更新外部 JavaScript/Css 文件
- JavaScript_动态加载CSS和JS文件
- 动态加载外部javascript文件的函数代码分享
- 动态加载外部javascript文件的函数代码分享
- 用javascript动态加载不同css/js文件
- 动态载入/删除/更新外部 JavaScript/Css 文件
- 使用js加载器动态加载外部Javascript文件
- 动态加载js和css(外部文件)
- 用JavaScript动态加载CSS和JS文件
- JavaScript 的性能优化:加载和执行(以及动态引入的外部 JS 文件在各浏览器中的加载顺序不一致)
- 如何动态加载外部Javascript文件
- 使用js加载器动态加载外部Javascript文件
- 在Javascript中动态加载css文件和js文件