国产探花免费观看_亚洲丰满少妇自慰呻吟_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: 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 伊川县| 通州区| 肃南| 富宁县| 勐海县| 来凤县| 韩城市| 衡阳县| 门头沟区| 沂南县| 阳原县| 泗洪县| 巩义市| 小金县| 四川省| 尉氏县| 盘山县| 柳林县| 武威市| 津市市| 自治县| 新田县| 准格尔旗| 甘泉县| 政和县| 新蔡县| 乐亭县| 余江县| 林州市| 阳新县| 普兰店市| 克什克腾旗| 江安县| 西平县| 柳林县| 太康县| 边坝县| 镇巴县| 金昌市| 鄂托克前旗| 盐边县|