非常簡潔的動態載入js和css的jquery plugin

來源:互聯網
上載者:User

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“,實現了基於角色許可權系統等功能。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.