↗☻【高性能网站建设进阶指南 #BOOK#】第5章 整合异步脚本

<!doctype html>

<html lang="zh-CN">

<head>

    <meta charset="utf-8" />

    <title></title>

</head>

<body>

    <script>

        if (window.addEventListener) {

            window.addEventListener('load', init, false);

        } else if (window.attachEvent) {

            window.attachEvent('onload', init);

        }



        // onreadystatechange在Internet Explorer中有效,onload在其他浏览器中有效,而Opera比较特殊,两者都有效,所以程序中需要做标记来处理

        function init() {

            console.log($);

        }

        var domscript = document.createElement('script');

        domscript.src = 'http://static01.baomihua.com/js/lib/jquery-1.4.4.min.js?t=20120926.js';

        domscript.onloadDone = false;

        domscript.onload = function() {

            if (! domscript.onloadDone) {

                domscript.onloadDone = true;

                init();

            }

        };

        domscript.onreadystatechange = function() {

            if (("loaded" === domscript.readyState || "complete" === domscript.readyState ) && ! domscript.onloadDone) {

                domscript.onloadDone = true;

                init();

            }

        };

        document.getElementsByTagName('head')[0].appendChild(domscript);



        function f() {

            console.log('f()');

        }

        var scriptdom = document.createElement('script');

        scriptdom.src = 'ca.js';

        if (-1 != navigator.userAgent.indexOf('Opera')) {

            scriptdom.innerHTML = 'f()';

        } else {

            scriptdom.text = 'f()';

        }

        document.getElementsByTagName('head')[0].appendChild(scriptdom);

    </script>

</body>

</html>

ca.js

function createMenu() {

    console.log('createMenu');

}

var scripts = document.getElementsByTagName('script');

var cntr = scripts.length;

while (cntr) {

    var curScript = scripts[cntr - 1];

    if (-1 != curScript.src.indexOf('ca.js')) {

        eval(curScript.innerHTML);

        break;

    }

    cntr--;

}

你可能感兴趣的:(网站建设)