随笔-5  评论-41  文章-13  trackbacks-0

为javascript增加import方法,用于引入需要的js文件。通过增加element的方法进行添加的方法是从网上得来的。我将原来作为参数传进来的type改成了根据文件名后缀来判断,看起来更加直观一些。
 1/*--------------------------------------------------------------------------+
 2   | JSLoad (url           // String  - Scripts location (i.e. http:///a.js)
 3   |         [, container] // Object  - Window with script loading capability
 4   |         [, type]      // String  - Type of script (i.e. text/javascript)
 5   |         [, defer]     // Boolean - Flag for delaying script processing
 6   |         [, language]  // String  - Language script is written in.
 7   |         [, title])    // String  - Title for loaded script
 8   |+--------------------------------------------------------------------------+
 9   | Loads external JavaScripts; used by Import.
10   *--------------------------------------------------------------------------*/

11   function JSLoad(url, container, type, defer, language, title)
12   {
13      // verify / attain container
14      if(container == undefined || container == null) container = this;
15
16      // setup container
17      if(typeof container.write == "undefined")
18         if(typeof container.document != "undefined")
19            container = container.document;
20         else throw "Invalid container. Unable to load [" + url + "]";
21
22      // no type set
23      if(type == undefined || type == null)
24      {
25         type = '';
26
27         // no language so set default type
28         if(language == undefined || language == null)
29         {
30            language = undefined;
31            type = "text/javascript";
32         }

33      }

34
35      // set default language
36      if(language == undefined || language == null) language = "JavaScript";
37
38      // set title
39      if(title == undefined || title == null) title = '';
40
41      // set defer
42      if(defer == undefined) defer = false;
43
44      // build the script object
45      var script = container.createElement("script");
46      script.defer = defer;
47      script.language = language;
48      script.title = title;
49      script.type = type;
50      script.src = url;
51
52      // dynamically load the script via it's container
53      var head = container.getElementsByTagName("head")[0];
54      head.appendChild(script);
55   }

使用方法:$import("test.js");
posted on 2006-12-18 16:45 OO 阅读(3876) 评论(0)  编辑  收藏 所属分类: java相关的乱七八糟的东西

只有注册用户登录后才能发表评论。


网站导航: