1// plugin author : chenjinfa@gmail.com
2// plugin name : $.include
3// $.include('file/ajaxa.js');$.include('file/ajaxa.css');
4// or $.includePath = 'file/';$.include(['ajaxa.js','ajaxa.css']);
5
6$.extend({
7 includePath: '',
8 include: function(file) {
9 var files = typeof file == "string" ? [file]:file;
10 for (var i = 0; i < files.length; i++) {
11 var name = files[i].replace(/^\s|\s$/g, "");
12 var att = name.split('.');
13 var ext = att[att.length - 1].toLowerCase();
14 var isCSS = ext == "css";
15 var tag = isCSS ? "link" : "script";
16 var attr = isCSS ? " type='text/css' rel='stylesheet' " : " language='javascript' type='text/javascript' ";
17 var link = (isCSS ? "href" : "src") + "='" + $.includePath + name + "'";
18 if ($(tag + "[" + link + "]").length == 0) document.write("<" + tag + attr + link + "></" + tag + ">");
19 }
20 }
21});
放在javascript檔案頭部,就像C#(using...)或Java(import ...)
樣本:部門管理頁 Department.aspx
註:因編輯器的問題,內容(http://www.cnblogs.com)原文是“. . / . . /”
.
<head>
<link rel="stylesheet" href="http://www.cnblogs.com/css/common.css" type="text/css"/>
<script src="http://www.cnblogs.com/javascript/jquery.js" type="text/javascript"></script>
<script src="http://www.cnblogs.com/javascript/common.js" type="text/javascript"></script>
<script src="DepartmentInfo.js" type="text/javascript"></script>
</head>
include plugin 代碼 放在公用common.js。
DepartmentInfo.js 檔案代碼
$.includePath = 'http://www.cnblogs.com/javascript/';
$.include(['json2.js', 'jquery.tree.js', 'jquery.tree.css']);
$(function() {
$("#depTree").tree({
url: "http://www.cnblogs.com/service/ES_SYSTEM.svc/DEP_INFO_Tree"
});
.
});
.
這樣寫法挺好看的。
以後有空再我發表我寫的整個項目及體會。基於asp.net Data Service 和 jquery的一套架構系統, 名稱定為:“E-Frame“,實現了基於角色許可權系統等功能。