JS對外部檔案的載入及對IFRMAME的載入的實現,當載入完成後,指定指向方法(方法回調)_javascript技巧

來源:互聯網
上載者:User

方法回調: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.jb51.net;
if (iframe.attachEvent) {
iframe.attachEvent("onload", function () { // …你的代碼邏輯 }); } else {
iframe.onload = function () {
// …你的代碼邏輯
};
}
document.body.appendChild(iframe);
</script>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.