zoukankan      html  css  js  c++  java
  • JS对外部文件的加载及对IFRMAME的加载的实现,当加载完成后,指定指向方法(方法回调)

    方法回调:callback方法回调是指当某方法执行完成后,去自动执行指定的另一方法的过程.下面举两个代表性的例子,说说JS世界里的方法回调.

    一 对JS脚本文件动态加载,当加载完成后,去回调一个函数

    <script>
           /* js动态加载脚本库方法 */
            function include_js(file) {
                var _doc = document.getElementsByTagName('head')[0];
                var js = document.createElement('script');
                js.setAttribute('type', 'text/javascript');
                js.setAttribute('src', file);
                _doc.appendChild(js);
                if (!/*@cc_on!@*/0) { //if not IE 
                    //Firefox2、Firefox3、Safari3.1+、Opera9.6+ support js.onload 
                    js.onload = function () {
                        // …你的代码逻辑
                    }
                } else { //IE6、IE7 support js.onreadystatechange
                    js.onreadystatechange = function () {
                        if (js.readyState == 'loaded' || js.readyState == 'complete') {

    // …你的代码逻辑

    //加载Jquery脚本库,完成后,执行jquery里的方法

                            $("#div1").html("ok");
                        }
                    }
                }
                return false;
            } //execution function
            include_js('http://img1.c2cedu.com/Scripts/jquery/jquery-1.4.2.min.js');
        </script>

    二 动态加载IFRAME框架页,当加载完成后,去回调一个函数

    <script>
            var iframe = document.createElement("iframe");
            iframe.src = "http://www.sina.com";
            if (iframe.attachEvent) {
                iframe.attachEvent("onload", function () {
                   // …你的代码逻辑
                });
            } else {
                iframe.onload = function () {
                    // …你的代码逻辑
                };
            }
            document.body.appendChild(iframe);
        </script>
  • 相关阅读:
    font-weight:bolder与设置数值的区别
    纯CSS3打造圆形菜单
    CSS Specificity
    控制页面内跳转
    解决Python操作MySQL中文乱码的问题
    字体图标font-awesome
    linux下安装使用MySQL 以及 python mysqldb 遇到的问题
    CocosCreator游戏开发(二)SocketIO简易教程
    CocosCreator游戏开发---菜鸟学习之路(一)资料整理
    2017已经接近尾声,然而我却什么都没干成
  • 原文地址:https://www.cnblogs.com/lori/p/2097588.html
Copyright © 2011-2022 走看看