欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

How to Auto Include a Javascript File

 更新時(shí)間:2007年02月02日 00:00:00   作者:  
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: 

復(fù)制代碼 代碼如下:

function getXMLHttpObj(){ 
  if(typeof(XMLHttpRequest)!='undefined') 
    return new XMLHttpRequest(); 

  var axO=['Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.4.0', 
    'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'], i; 
  for(i=0;i<axO.length;i++) 
    try{ 
      return new ActiveXObject(axO[i]); 
    }catch(e){} 
  return null; 


Most browsers other than Internet Explorer 5 or 6 have a built-in XMLHttpRequest object. Internet Explorer 7, when it's released, will also have this object natively. The first thing we do is check to see if this object exists. If it does, we create an instance of it and that's it. If the object doesn't exist, we attempt to create one of several ActiveX Objects. We don't know what objects our users have installed, so we attempt to create several different XMLHTTP objects, starting with the newest ones. 

Now in order to dynamically load functions, we first need to define them. We could do this one function at a time, but instead of hard-coding dozens of functions, we can choose to just make an object or array with all the file names and the functions you want to have auto-included: 

復(fù)制代碼 代碼如下:

var autoIncludeFunctions = { 
  'scripts/file1.js': ['function1', 'function2', 'function3'], 
  'scripts/file2.js': ['function4', 'function5', 'function6'], 
  'scripts/file3.js': ['function7', 'function8', 'function9'] 



Our autoIncludeFunctions object should contain a list of JavaScript files, as well as a list of functions in those files. Here we are using shorthand JavaScript notation to create both the object and the arrays, but the same thing could be accomplished in many different ways. 

These .js files can contain any code you have available, such as JavaScript menus, animations, etc. The simplest example would be a file titled "file1.js" that only contained "function function1(){ alert('Hello, World!'); }".

Note that if any of these files contain functions with the same name as another file, only the last function listed will be used. 

To make things a bit easier, we're going to make a function that will pull a JavaScript file down and execute it. It's very important, in our case, that the third paramater sent to the XMLHTTP object be false. This forces the script to wait for the response to download as opposed to continuing on with other code. 

復(fù)制代碼 代碼如下:

function loadScript(scriptpath, functions){ 
  var oXML = getXMLHttpObj(); 
  oXML.open('GET', scriptpath, false); 
  oXML.send(''); 
  eval(oXML.responseText); 
  for(var i=0; i<functions.length; i++) 
    window[functions[i]] = eval(functions[i]); 

The loadScript function expects two arguments: scriptpath and functions. "scriptpath" should contain the URL to your JavaScript file, and "functions" is the array of functions that exist in this JavaScript file.

As you can see, the code to pull and execute a script is straightforward. The browser first downloads, and then interprets the JavaScript file. If you've read any other articles on AJAX development, you might remember that in most cases the third argument sent to the open() function of an XMLHTTP object is usually "true." In our case we have it set to "false." This argument controls the state of the XMLHTTP object. If set to true, the object runs asynchrounously, meaning that all other JavaScript code continues while the object is loading. While this is a good thing in many circumstances, if we implemented it here our code would return before our file was done loading. Since we want our code to wait until this file is complete, we set this third argument to false, thus pausing our JavaScript execution until we are ready to continue. 

When the code is evaluated from the responseText, it's executed in the limited scope of the loadScript function and because of this, none of these functions will be available outside of the loadScript function. In order do get around this, the for loop adds each function to the window object, thus making it globally available. 

It's important to note that only scripts on the same server as the current page can be called in this manner. This is inherent to the XMLHTTP Object and is a necessary measure to increase general browser security. 

相關(guān)文章

  • JS中的變量作用域(console版)

    JS中的變量作用域(console版)

    這篇文章主要介紹了JS中作用域以及變量范圍,需要的朋友可以參考下
    2020-07-07
  • echarts使用中關(guān)于y坐標(biāo)軸無(wú)法正常顯示的問(wèn)題解決記錄

    echarts使用中關(guān)于y坐標(biāo)軸無(wú)法正常顯示的問(wèn)題解決記錄

    Echarts是由百度提供的數(shù)據(jù)可視化解決方案,下面這篇文章主要給大家介紹了關(guān)于echarts使用中關(guān)于y坐標(biāo)軸無(wú)法正常顯示的問(wèn)題解決記錄,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • Vue3基于countUp.js實(shí)現(xiàn)數(shù)字滾動(dòng)的插件

    Vue3基于countUp.js實(shí)現(xiàn)數(shù)字滾動(dòng)的插件

    本文主要介紹了Vue3基于countUp.js實(shí)現(xiàn)數(shù)字滾動(dòng)的插件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Javascript獲取表單名稱(name)的方法

    Javascript獲取表單名稱(name)的方法

    這篇文章主要介紹了Javascript獲取表單名稱(name)的方法,涉及javascript操作表單的技巧,需要的朋友可以參考下
    2015-04-04
  • 一文帶你探索JavaScript中斜杠的神秘世界

    一文帶你探索JavaScript中斜杠的神秘世界

    斜杠可以在代碼中擁有多種含義,所以在 JavaScript 編程中,處理斜杠是一項(xiàng)非常重要的任務(wù),本文就來(lái)帶大家一起來(lái)看看JavaScript是如何處理斜杠的
    2023-06-06
  • 微信js-sdk 錄音功能的示例代碼

    微信js-sdk 錄音功能的示例代碼

    這篇文章主要介紹了微信jsdk錄音功能的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Three.js利用性能插件stats實(shí)現(xiàn)性能監(jiān)聽(tīng)的方法

    Three.js利用性能插件stats實(shí)現(xiàn)性能監(jiān)聽(tīng)的方法

    Three.js 是一款運(yùn)行在瀏覽器中的 3D 引擎,你可以用它創(chuàng)建各種三維場(chǎng)景,而下面這篇文章主要給大家介紹了關(guān)于Three.js如何利用性能插件stats實(shí)現(xiàn)性能監(jiān)聽(tīng)的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-09-09
  • JavaScript 數(shù)組中最大最小值

    JavaScript 數(shù)組中最大最小值

    本文給大家匯總介紹的是獲取JavaScript 數(shù)組中最大最小值的方法和示例,非常的詳細(xì)和全面,希望對(duì)大家學(xué)習(xí)JavaScript能夠有所幫助
    2016-06-06
  • 微信小程序路由跳轉(zhuǎn)兩種方式示例解析

    微信小程序路由跳轉(zhuǎn)兩種方式示例解析

    這篇文章主要為大家介紹了微信小程序路由跳轉(zhuǎn)兩種方式示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2022-04-04
  • js中創(chuàng)建對(duì)象的幾種方式

    js中創(chuàng)建對(duì)象的幾種方式

    本文主要介紹了js中創(chuàng)建對(duì)象的幾種方式。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧
    2017-02-02

最新評(píng)論