app.js ```javascript /** * 适用于 自动加载的js */ var App = {}; App.init = function() {}; (function($, App) { var DemoBox = function(box) { this.box = box; this.bind(); this.search(); }; DemoBox.init = function() { var box = $("#Demo"); if(box.attr('id')) { return new DemoBox(box); } else { return null; } }; DemoBox.prototype.bind = function() { var self = this; this.box.find('.onclick').bind('click', function() { }); }; DemoBox.prototype.bind = function() { var self = this; // this.box.find('.delete').bind('click', function() { // var choose = confirm('您确定删除该用户?'); // if (!choose) { // return false; // } // // var userId = $(this).prev().val(); // $.post('/user/delete', {id: userId}, function(ret) { // if (ret.error) return false; // window.location.reload(true); // }, 'json'); // }); }; DemoBox.prototype.search = function() { var self = this; this.box.find('.search').bind('click', function() { var q = $(this).parent().prev('.search-q').val(); window.location.href = "/project?q="+q; }); }; //调用js方法 App.DemoBox = DemoBox.init(); })(jQuery, App); //执行该文件下js $(document).ready(function() { App.init(); }); ``` load.js ```javascript /** * 用户外部调用 才执行的js */ /** * A/B Test client */ (function($, window, document, undefined) { var AB; $.AB = AB = {}; /** * A/B 测试 * @param {String} name 名称标示,用于唯一标示需要渲染的页面块 * @param {Function} cb 后处理函数,在页面渲染之后完成调用 */ var Tester = function(name, cb) { if (!cb || typeof cb != 'function') { cb = function() {}; } this.name = name; this.url = document.URL; this.loadCookie(); this.loadChoice(cb); }; /** * 常量 */ Tester.SERVER_CHOICE_URI = '/ab-choice'; Tester.SERVER_REDIRECT_URL = 'http://ab.medlive.cn/r'; /** * 加在服务器派发的选择,并渲染页面 * @param {Function} cb 后处理函数 */ Tester.prototype.loadChoice = function(cb) { var self = this; var data = { module: this.name, abid: this.abid, url: this.url }; $.post(Tester.SERVER_CHOICE_URI, data, function(ret) { if (ret.error) { return cb(ret.error); } var cDiv = $('#' + ret.cid); if (cDiv.length == 0) { return cb('div is not exists' + ret.cid); } cb(); if (ret.abchoice) { Cookies.set('abchoice', ret.abchoice, { expires: 1, path: '/', domain: 'medlive.cn' }); } self.bindLinks(cDiv, ret.abchoice); cDiv.show(); }, 'json'); }; /** * 对页面中的链接点击跳转逻辑重写 * @param {Object} cDiv 需要显示的页面块 */ Tester.prototype.bindLinks = function(cDiv, abchoice) { var self = this; cDiv.find('a').each(function(e) { var href = $(this).attr('href'); var params = [ 'url_from=' + encodeURIComponent(self.url), 'url_to=' + encodeURIComponent(href), 'abid=' + self.abid, 'abchoice=' + abchoice, 'name=' + self.name, ]; href = Tester.SERVER_REDIRECT_URL + '?' + params.join('&'); $(this).attr('href', href); }); }; /** * 生成cookie标记,用于A/B测试系统标示用户 * @param {Number} length 长度 * @return {String} 随机序列 */ Tester.prototype.genId = function(length) { var chars = '0123456789abcdefghijklmnopqrstuvwxyz'; var result = ''; for (var i = length; i > 0; --i) { result += chars.charAt(Math.round(Math.random() * (chars.length - 1))); } return result; }; /** * 生成cookie,键值为 abid️ */ Tester.prototype.loadCookie = function() { var abid = Cookies.get('abid'); if (!abid) { abid = this.genId(40); Cookies.set('abid', abid, { expires: 7, path: '/', domain: 'medlive.cn' }); } this.abid = abid; }; /** * A/B接口方法 * @param {String} name 名称标示,用于唯一标示需要渲染的页面块 * @param {Function} cb 后处理 * @return {Object} */ Tester.load = function(name, cb) { if (!name) { return null; } // var obj = $('#' + name); // if (obj.length == 0) { // return null; // } new Tester(name, cb); }; // A/B test api AB.load = Tester.load; })(jQuery, window, document); ``` index.html ```html !Templates.000000.content! javascript 闭包形式 ```