zoukankan      html  css  js  c++  java
  • 异步加载js,Css方法

    <!doctype html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>Loading Script and Style File</title>
    </head>
        <body>
            <div id="out">
            </div>
            <script>
                //加载ok.css
                AsynLoadCss('ok.css','body', function () {
                
                });
                //加载jquery
                AsynLoadJs('jquery-1.7.1.js','body', function () {
                    //加载完成后的代码!
                    $(document).ready(function () {
                        $("#out").html("加载完成后的代码!");
                    });
                    
                });
                
            //异步加载js方法
            function AsynLoadJs(src, target, callback) {
                var node = document.createElement('script'),
                        outEl;
                switch (target) {
                    case 'body':
                        outEl = document.body;
                        break;
                    case 'head':
                        outEl = document.getElementsByTagName('head')[0];
                        break;
                    default:
                        outEl = document.getElementsByTagName('head')[0];
                }
                node.type = 'text/javascript';
                if (node.addEventListener) {
                    node.addEventListener('load', callback, false);
                    node.addEventListener('error', function () {
                        //error function
                    }, false);
                }
                else { // for IE6-8
                    node.onreadystatechange = function () {
                        var rs = node.readyState;
                        if (rs === 'loaded' || rs === 'complete') {
                            node.onreadystatechange = null;
                            callback();
                        }
                    };
                }
                node.src = src;
                outEl.appendChild(node);
            }
            //异步加载Css方法
            function AsynLoadCss(src, target, callback) {
                var node = document.createElement('link'),
                        outEl;
                switch (target) {
                    case 'body':
                        outEl = document.body;
                        break;
                    case 'head':
                        outEl = document.getElementsByTagName('head')[0];
                        break;
                    default:
                        outEl = document.getElementsByTagName('head')[0];
                }
                node.rel = "stylesheet";
                node.type = 'text/css';
                if (node.addEventListener) {
                    node.addEventListener('load', callback, false);
                    node.addEventListener('error', function () {
                        //error function
                        //callback();
                    }, false);
                }
                else { // for IE6-8
                    node.onreadystatechange = function () {
                        var rs = node.readyState;
                        if (rs === 'loaded' || rs === 'complete') {
                            node.onreadystatechange = null;
                            callback();
                        }
                    };
                }
                node.href = src;
                outEl.appendChild(node);
            }

            </script>
        </body>
    </html>

  • 相关阅读:
    第40次全国计算机等级考试监考
    [再寄小读者之数学篇](2014-07-27 打印错误吧)
    日积月累的名典[2014-10-7]
    2014年全球“高被引科学家”数学类名单
    年轻尼姑的19句话
    PostgreSQL的 initdb 源代码分析之十六
    PostgreSQL的 initdb 源代码分析之十五
    PostgreSQL的 initdb 源代码分析之十四
    PostgreSQL的 initdb 源代码分析之十三
    PostgreSQL的 initdb 源代码分析之十二
  • 原文地址:https://www.cnblogs.com/lzf0514/p/2721453.html
Copyright © 2011-2022 走看看