国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > JS > 正文

How to Auto Include a Javascript File

2024-09-06 12:43:31
字體:
來源:轉載
供稿:網友
Form: http://www.webreference.com/programming/javascript/mk/
Author:Mark Kahn

Many developers have a large library of JavaScript code at their fingertips that they developed, their collegues developed, or that they've pieced together from scripts all over the Internet. Have you ever thought that it would be nice to not have to search through all those files just to find that one function? This article will show you how dynamically include any JavaScript file, at runtime, by simply calling a function in that file!

Here's an example: You have a function foo() in file bar.js. In your code, you know that foo() might be called, but it probably won't be because most people do not use its functionality. You don't want to force the user to download bar.js unless it's going to be used because it's a fairly large file. Here you'll learn how to make a fake foo() function that actually loads bar.js on the fly and then calls the real foo() function.

Dynamically Loading a Script
As many developers know, there are at least two different ways to dynamically load a script at runtime. The first is to create a script object and append it to the document. The second is to use an XMLHTTP request to grab the source code, and then eval() it. 

It is this second method that we're going to use, and we're going to exploit the fact that an XMLHTTP request has the capability to completely stall any script activity. 

First, some basics: how to create an XMLHTTP Object. There are as many different functions to return a cross-browser XMLHTTP Object as there are developers that work with AJAX. I happen to have my own as well, and here's a simplified example of that: 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 株洲县| 新乡县| 荥经县| 平顶山市| 会东县| 玛曲县| 临西县| 门头沟区| 靖江市| 衡水市| 阿勒泰市| 宕昌县| 佳木斯市| 环江| 红桥区| 灵寿县| 乐都县| 二手房| 会理县| 瑞昌市| 江都市| 承德县| 云龙县| 仲巴县| 彭山县| 荥阳市| 隆化县| 舒兰市| 长宁县| 康乐县| 河西区| 揭西县| 务川| 遂平县| 宁强县| 邛崃市| 三门县| 谢通门县| 敦化市| 盐津县| 哈巴河县|