- (function(){
- if(!window.utils){
- window.utils = {};
- }
- var utils = window.utils;
- utils.setCookie=function(c_name,value,expiredays,httponly){
- // var domain = document.location.host;
- var exdate=new Date()
- exdate.setDate(exdate.getDate()+expiredays);
- document.cookie=c_name+ "=" +escape(value)+ ((typeof expiredays == "undefined") ? "" : ";expires="+exdate.toGMTString())+";path=/;";
- };
- utils.getCookie=function(c_name){
- if (document.cookie.length>0){
- c_start=document.cookie.indexOf(c_name + "=");
- if (c_start!=-1){
- c_start=c_start + c_name.length+1 ;
- c_end=document.cookie.indexOf(";",c_start);
- if (c_end==-1){
- c_end=document.cookie.length;
- }
- return unescape(document.cookie.substring(c_start,c_end));
- }
- }
- return "";
- };
- utils.formToObj = function(form){
- var o = {};
- $("input,textarea,select",form).each(function(index) {
- var val = '';
- if($(this).is(":checkbox")){
- val = $(this).is(":checked");
- }else if($(this).is(":radio")){
- if($(this).is(":checked")){
- val = $(this).val();
- }
- } else {
- val = $(this).val();
- }
- if (o[this['name']]) {
- o[this['name']] = o[this['name']] + (val?"," + val:"");
- } else {
- o[this['name']] = val;
- }
- });
- return o;
- };
- utils.objToForm =function (form,obj){
- for ( var name in obj) {
- if($("input:radio")){
- $("input[name='"+name+"'][value='"+obj+"']").prop({"checked":"checked"});
- }else{
- $("input[name='"+name+"'],textarea[name='"+name+"'],select[name='"+name+"']",form).val(obj[name]);
- }
- }
- };
- utils.messageFormat = function(text,array){
- var v = text.match(/\\{\\d+\\}/g);
- if(v===null){
- return text;
- }
- for(var i=0;i<v.length;i++){
- var j = v[i].toString().match(/\\d+/);
- text = text.replace(new RegExp("\\\\"+v[i],"g"),array[j]);
- }
- return text;
- };
- utils.messageFormat2 = function(text,obj){
- if(obj && typeof obj == "object"){
- for(var key in obj){
- var reg = new RegExp("\\\\{"+key+"\\\\}","g");
- text = text.replace(reg,obj[key]);
- }
- }
- return text;
- };
- })();
- //该片段来自于http://www.codesnippet.cn/detail/3004201512463.html
来源: http://www.codesnippet.cn/detail/3004201512463.html