JavaScript 代码片段
1.无题
if (i && i.charAt(i.length - 1) == "/") { i = i.substr(0, i.length - 1) }
2.无题
if(typeof(jQuery) !== 'undefined'){ getUserInfo(); }
3.无题
sorted: function(a, b) { // sort alphabetically in ascending order return a.label == b.label ? 0 : ( a.label > b.label ? 1 : -1 ) }
4.JavaScript模拟QueryString获取GET参数
QueryString = { data: {}, Initial: function () { var aPairs, aTmp; var queryString = new String(window.location.search); queryString = queryString.substr(1, queryString.length); //remove "?" aPairs = queryString.split("&"); for (var i = 0; i < aPairs.length; i++) { aTmp = aPairs[i].split("="); this.data[aTmp[0]] = aTmp[1]; } }, GetValue: function (key) { return this.data[key]; } }
5.JavaScript使用正则表达式替换括号、尖括号等
/*正则表达式 替换括号,尖括号等*/ function toTxt(str) { var RexStr = /\<|\>|\"|\'|\&/g str = str.replace(RexStr, function(MatchStr) { switch (MatchStr) { case "<": return "<"; break; case ">": return ">"; break; case "\"": return """; break; case "'": return "'"; break; case "&": return "&"; break; default: break; } }) return str; }
===========================我是分割线==============================
(function(){ function isArray(obj) { return Object.prototype.toString.call(obj) === '[object Array]'; } function loadFunction(list) { var list = isArray(list) ? list : []; for (var i=0; i<list.length; i++) { window[list[i]] = window[list[i]] || function(){}; } } //执行loadFunction函数,传递一个数组列表 loadFunction([ 'cmSetClientID', 'cmCreatePageviewTag', 'cmCreateProductviewTag', 'cmCreateShopAction9Tag', 'cmCreateOrderTag', 'cmCreateRegistrationTag', 'cmCreateElementTag', 'cmCreateConversionEventTag' ]); // coremetrics预定义方法 window["_LVMAMA_COREMETRICS"] = window["_LVMAMA_COREMETRICS"] || {}; /** * 部署环境参数: _LVMAMA_COREMETRICS.CURRENT_ENV * * 开发: DEVELOPMENT; * 生产: PRODUCTION; */ _LVMAMA_COREMETRICS.CURRENT_ENV = 'PRODUCTION'; _LVMAMA_COREMETRICS.ENV = { DEVELOPMENT: [ '60409730', false, 'testdata.cn.coremetrics.com' ], PRODUCTION: [ '90409730', true, 'data.cn.coremetrics.com' ] }; _LVMAMA_COREMETRICS.init = function(domain) { cmSetClientID.apply(null, _LVMAMA_COREMETRICS.ENV[_LVMAMA_COREMETRICS.CURRENT_ENV].concat("lvmama.com")); } })();