Dynamic load JS / CSS files

2010-06-17  来源:本站原创  分类:Web  人气:223 

Dynamic load JS / CSS file, you can determine whether loaded.

function loadScript(url, callback) {
        var script = document.createElement("script")
        script.type = "text/javascript";
        if (script.readyState) { // IE
                script.onreadystatechange = function() {
                        if (script.readyState == "loaded"
                                        || script.readyState == "complete") {
                                script.onreadystatechange = null;
                                callback();
                        }
                };
        } else { // Others
                script.onload = function() {
                        callback();
                };
        }
        script.src = url;
        document.getElementsByTagName("head")[0].appendChild(script);
}

// Example
loadScript("file1.js", function(){
    alert("File is loaded!");
});
// Order load
loadScript("file1.js", function() {
                loadScript("file2.js", function() {
                        loadScript("file3.js", function() {
                                alert("All files are loaded!");
                        });
                });
});
相关文章
  • Dynamic load JS / CSS files 2010-06-17

    Dynamic load JS / CSS file, you can determine whether loaded. function loadScript(url, callback) { var script = document.createElement("script") script.type = "text/javascript"; if (script.readyState) { // IE script.onreadystatechange

  • Dynamic load JS / CSS files - improved version 2011-01-11

    function loadScript(url, callback,targetWin) { if(!targetWin) targetWin = self; var script = targetWin.document.createElement("script") script.type = "text/javascript"; if (script.readyState) { // IE script.onreadystatechange = functio

  • Dynamic load JS / CSS 2010-01-26

    1, dynamic load JS var head = document.getElementsByTagName('HEAD').item(0); var script = document.createElement('script'); script.src = 'script.js'; script.type = 'text/javascript'; head.appendChild(script); Second, dynamic loading CSS var head = do

  • Simple dynamic load js and css 2010-06-29

    function importJs (filename) ( var ref = document.createElement ("script"); ref.setAttribute ("type", "text / javascript"); ref.setAttribute ("src", filename); if (typeof ref! = undefined) document.getElementsByTagN

  • mvn jetty locks js, css files 2010-04-11

    Grails transferred recently from the spring mvc, former grails of mvn grails: run-app has been using very high, Now mvn jetty: run with very bad mood, the biggest problem is the jetty will be locked css, jss file, A specific solution can see the foll

  • jquery dynamic load css style sheet 2010-08-06

    Achieved by dynamic load jquery css style sheet or replace $.extend({ csslzLoad: function(cssfileurl,styleobj){ var linkobj; if( !!styleobj && $(styleobj).size()>0 && $(styleobj).attr("tagName")=='LINK' ){ linkobj=$(styleobj);

  • WEB development of high-performance (7) - JS.CSS merger. Compression. Cache management 2010-06-23

    This article focuses on under the current JS, CSS consolidation, compression, cache management some problems, and then share my project used a treatment program, and provides an example of download. Problems: Combined, compressed files are mainly two

  • Website performance optimization - JS.CSS merger. Compression. Cache Management 2010-12-16

    Zhuantie: Website performance optimization - JS, CSS consolidation, compression, cache management This article discusses the current under the JS, CSS merge, compress, cache management problems exist, and then share their own projects under the one u

  • js refresh the multi-level dynamic load css reference 2010-05-26

    Problem description: Page reference page.css, page references under the then (@ import) a.css, a.css contexts a1.css, a2.css, a3.css .... (Ie to allow reference to four, four invalid after each. Css reference allows a maximum of 31 css files) If ther

  • Javascript load Page,load css,load js实现代码 2013-11-01

    通过js动态载入页面和css或js的实现代码,需要的朋友可以参考下.国外人写的,可以参考下. /*********************************************** * Ajax Page Fetcher- by JavaScript Kit (www.javascriptkit.com) ***********************************************/ var ajaxpagefetcher={ loadingmessage: "Loa

  • To prevent the browser cache css, js static files [transfer] 2011-01-11

    Under what circumstances, to prohibit the static file cache: 1, is often possible to change the js, css. Such as a html file, test.html in the 1.0 version. It may be so <script src="common.js"> </ script> The revised version v1.1: &l

  • High-performance web development - how to load JS, JS should be placed in what position? [Change] 2010-04-12

    Download blocking external JS http://www.blogjava.net/BearRui/archive/2010/04/08/web_performance_js_where.html JS browser to download all the time, would prevent all other activities, such as other resources, downloads, content presentation and so on

  • Web Performance Tuning: JavaScript and CSS files combined 2010-11-15

    Best Practices in Web Performance Optimization of the most important one is to reduce the HTTP request, it is also the largest one in the proportion of YSlow rules. Programs to reduce HTTP requests are combined JavaScript and CSS files, CSS Sprites,

  • YUI compression JS, CSS 2009-12-16

    YUI Compression: Quote http://www.javaeye.com/topic/368724 Use two jar package YUIAnt.jar Download http://www.ubik-ingenierie.com/miscellanous/YUIAnt/ yuicompressor-2.4.2.jar Download http://www.julienlecomte.net/yuicompressor/ 1. <property name="

  • Dynamic load will cause memory leaks Javascript? Rss articles Subscribe 2010-06-01

    A few days ago in javaeye made a poster, "Dynamic load Javascript will lead to memory leak?", His article was to be foreigners out of the more confusing, the Internet has asked a few skilled are here to thank those who helped me, work is very bu

  • Access to Web pages with JavaScript js.css.Flash and other documents 2010-06-22

    Contents I. Introduction II E books or web pages from the files of the general steps to get three, from E to get the book or web page links to four incoming css files, books or web pages from E to get the js file link came in the five, from the E boo

  • Access to Web pages with JavaScript and other documents js.css.Flash 2010-06-22

    Contents I. Introduction II E books or web pages from the files of the general steps to get three, from E to get the book or web page links to four incoming css files, books or web pages from E to get the js file link came in five books from the E Ge

  • TBCompressor 2.4.2 Taobao team with the js css compression tool Wandering Drifter 2010-07-09

    TBCompressor 2.4.2 Taobao team with the js css compression tool Wandering Drifter Today, the quest for a beautiful website, more and more powerful functionality JavaScript css with the credit contributed to the content of the website or the special e

  • Consolidation in the service-side JavaScript and CSS files and compression 2010-11-02

    Web Performance Tuning Best Practices of the most important one is to reduce HTTP requests , it is also YSlow a rule in the largest proportion. Programs to reduce HTTP requests are combined JavaScript and CSS files, CSS Sprites , image map (Image Map

  • 使用YUI+Ant 实现JS CSS压缩 2014-02-10

    YUI库是一组工具和控件,它们用JavaScript写成, 为的是用DOM 脚本,DHTML和AJAX等技术创建丰富的网页交互式应用程序.YUI 基于BSD协议,对所有的使用方式都是免费的.YUI 项目包括YUI 库和两个创建时工具:YUI Compressor (压缩)和YUI Doc(JavaScripts代码的文档引擎) 今天研究了一下YUI yahoo开源框架,感觉很猛啊. 于是乎我做了一个YUI的ant实现,网上好多关于bat的实现,我就另辟蹊径,出个关于这个的ant实现,嘿嘿独一无二