JavaScript file referring/JavaScript 文件引用

Modern web developing languages such as ASP.NET, PHP even CSS have the syntax of importing other code files to current one.

  • ASP.NET:
    <!--#include file="inc_footer.aspx"-->

  • PHP:
    <?php include("menu.php"); ?>
    <?php require("wrongFile.php"); ?>

  • CSS:
    @import url("import1.css");

Unfortunately, JavaScript doesn’t provide similar statements. So it’s really hard to maintain the quickly growing number of .js files.

I ever tried to maintain them through kinds of server side ways, like:

  1. Create a web page(.aspx, .php) which accept a url parameter, according to the value of the parameter, the page concatenates the content of required .js files and returns.
    So I can link to those required .js files by adding a script link to page:
    <script type="text/javascript" src="abc.aspx?key=Ajax" />

  2. Design a custom control, which contains links to required .js files, and put the control at where those files are needed.

But those ways are a bit of complicated. If you just want to ensure that the required .js files have been loaded at somewhere in a piece of js code, I recommend the following approach.

  1. Create a default.js, or common.js or basic.js that most web pages would use. And insert the piece of code below to it:
       1: if (typeof (window.PO) === 'undefined') window.PO = {};
       2: if (typeof (PO.Url) === 'undefined') PO.Url = {};
       3:  
       4: PO.Url.Combine = function(baseUrl, relativeUrl) {
       5:     if (!baseUrl) return relativeUrl ? relativeUrl : null;
       6:     if (!relativeUrl) return baseUrl;
       7:  
       8:     var ri = 0;
       9:     var bi = baseUrl.length - 1;
      10:  
      11:     if (baseUrl.charAt(bi) == '/') {
      12:         baseUrl = baseUrl.substr(0, bi);
      13:     } else {
      14:         bi = baseUrl.lastIndexOf('/');
      15:         if (bi > -1 && baseUrl.indexOf('.', bi) > -1)
      16:             baseUrl = baseUrl.substr(0, bi);
      17:     }
      18:  
      19:     ri = relativeUrl.indexOf('../');
      20:     bi = baseUrl.lastIndexOf('/');
      21:     while (ri > -1 && bi > -1) {
      22:         relativeUrl = relativeUrl.substr(ri + 3);
      23:         baseUrl = baseUrl.substr(0, bi);
      24:  
      25:         ri = relativeUrl.indexOf('../');
      26:         bi = baseUrl.lastIndexOf('/');
      27:     }
      28:  
      29:     return baseUrl + '/' + relativeUrl;
      30: };
      31:  
      32: //Google's approach:
      33: //document.write(unescape('%3Cscript src="' + baseUrl + 'common.js" type="text/javascript"%3E%3C/script%3E'));
      34: PO.Require = function(relativeUrl) {
      35:     if (!relativeUrl) return;
      36:  
      37:     var scripts = document.getElementsByTagName('script');
      38:     if (!scripts || !scripts.length)
      39:         return;
      40:  
      41:     var reg = /Basic.js/i; //TODO: replace Basic.js with yours.
      42:     var url = 'Script/Main'; //TODO: change default base url;
      43:  
      44:     for (i = 0; i < scripts.length; i++) {
      45:         var s = scripts[i];
      46:         if (s.src && s.src.match) {
      47:             var m = s.src.match(reg);
      48:             if (null != m) {
      49:                 url = m.input.substr(0, m.index - 1);
      50:                 break;
      51:             }
      52:         }
      53:     }
      54:  
      55:     url = this.Url.Combine(url, relativeUrl);
      56:  
      57:     var s = document.createElement('script');
      58:     s.type = 'text/javascript';
      59:     s.src = url;
      60:  
      61:     var head = document.getElementsByTagName('head')[0];
      62:     head.appendChild(s);
      63: };
  2. Then at somewhere that current piece of code requires another code file, add a statement as in:
    PO.Require('../Common/DragDrop.js');

    Note: the path of required .js file (“../Common/DragDrop.js” in the example) is relative to the default.js.

And, consider what you can do with it in the following case:

You found a pretty accordion control of javascript, but it requires a couple of .js files.

You don’t think it’s a good idea to add script links of all required .js files to each page that use this control.

How about:

  • Create a new folder and put all .js file related to that accordion control in it.
  • Create a new .js file, let’s call it accordion.js, and add some code as in:
  •    1: PO.Require('../Accordion/eee.ui.js');
       2: PO.Require('../Accordion/eee.ui.fade.js');
       3: PO.Require('../Accordion/eee.ui.move.js');
       4: ...
  • Add a script link of default.js (the one contains the defination of PO.Require function) to page:
  • <script type="text/javascript" src="/Script/default.js" />

  • Add a script link of newly created accordion.js:
  • <script type="text/javascript" src="/Script/Controls/accordion.js" />

posted on 2009-09-05 03:09 BFL 阅读(1213) 评论(0) 编辑 收藏

http://www.cnblogs.com/BFLForever/