js常用算法

1.格式化数字 该函数格式化数字样式为带有千分号和制定小数点数位的格式。

function fomatNumber(s, n) {
    n = n >= 0 && n <= 20 ? n : 2;
    s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
    var l = s.split(".")[0].split("").reverse(),
    r = s.split(".")[1];
    t = "";
    for(i = 0; i < l.length; i ++ ) {
      t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
    }
    if( n==0 ) 
        return t.split("").reverse().join("")
    else
        return t.split("").reverse().join("") + "." + r;
}

 

2.2010-10-13  当应用ajax时,iframe 在ie里面不能刷新 ,或者称之为有缓存我算是领教过了。本来应该轻松的今天浪费了我太多的时间。

如果A网页里有iframe,而这个iframe的src只想B页。当A刷新时,B中有ajax请求的话,则B不会刷新。只是在IE中才如此。解决办法唯有让iframe的src引用页的ajax请求的url每次都不一样 。在手头上的项目,不想让每个人都去改自己的页面去。所以我的办法只能是去更改jquery的源代码,让它的ajax请求每次都自动在后面加一个随机参数了。

 

3.2010-10-15 本来以为没有办法。但今天一查,却给了自己一个惊喜。原来在主页面也能判断何时该页面中的iframe加载完毕 ,从而可以为应用提供类似假滚动条的东西来增强用户体验。对于现在我设计的以iframe为单位的页面架构提供了以后让人无可挑剔的体验。方法可参考如下代码

<html>
 <head>
 </head>

 <body>
  
<div id="msg"></div>
<iframe id="detail" name="detail"></iframe>

 </body>

<script>
/**/
var div = document.getElementById("msg");
div.innerHTML = "数据加载中...";

var iframe = document.getElementById("detail");
iframe.width="100%";
iframe.height="100%";
iframe.src = "http://163.com";


if (iframe.attachEvent){
    iframe.attachEvent("onload", function(){
        div.style.display = "none";
    });
} else {
    iframe.onload = function(){
        div.style.display = "none";
    };
}
</script>

</html>

 

4.获得某个月天数的算法 。原本是给组内人员写个取得上月同期和上周同期的公用方法。取得上月同期,不得不先写这个算法了。

/**
 * get days number in month
 * @param year
 * @param month
 * @return
 */
function baseGetDatesInMonth(year, month) {
	if(parseInt(month) == 1 || parseInt(month) == 3 || parseInt(month) == 5 || parseInt(month) == 7
	     || parseInt(month) == 8 || parseInt(month) == 10 || parseInt(month) == 12) {
	    return 31;
	} else if(month == 2 && (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))) {
	    return 29;
	} else if(month == 2) {
	    return 28;
	} else {
	    return 30;
	}
}

/**
 * get the same date in last month
 * if last month does not contain the day the last day will return
 * @param thedate
 * @param n if n == 6, MM-dd will return
 * @return 
 */
function baseGetLastMonthTheSameDay(thedate, n) {
	var year = thedate.substring(0,4);
	var month = thedate.substring(5,7);
	var date = thedate.substring(8);
    
	var monthDays = baseGetDatesInMonth(year, month);

	var lastMonthYear = year;
	var lastMonth = month - 1;
	if(lastMonth == 0) {
	    lastMonthYear = year - 1;
		lastMonth = 12;
	}
	
	var lastMonthDays = baseGetDatesInMonth(lastMonthYear, lastMonth);

	var lastMonthDate = date;
	if(lastMonthDays < date) {
	    lastMonthDate = lastMonthDays;
	}
	if(lastMonth < 10) {
	    lastMonth = "0" + lastMonth;
	}
	/*
	if(lastMonthDate < 10) {
	    lastMonthDate = "0" + lastMonthDate;
	}
	*/
	
    
	if(n == 5) {
	    return lastMonth + "-" + lastMonthDate;
	}
    return  lastMonthYear + "-" + lastMonth + "-" + lastMonthDate;
}

/**
 * get the same date in last week
 * @param thedate
 * @param n if n == 6 MM-dd will return
 * @return
 */
function baseGetLastWeekTheSameDay(thedate, n) {
    var year = thedate.substring(0,4);
	var month = thedate.substring(5,7);
	var date = thedate.substring(8);

	var theDate = new Date();
    theDate.setYear(year);
	theDate.setMonth(month -1);
	theDate.setDate(date);

	var time = theDate.getTime();
	time = time - 7 * 24 * 60 * 60 * 1000
	theDate.setTime(time);
	
	var lastWeekYear = theDate.getYear();
	var lastWeekMonth = theDate.getMonth() + 1;
	var lastWeekDate = theDate.getDate();

    
	if(lastWeekYear < 1900) {
	    lastWeekYear = lastWeekYear + 1900;
    }
	if(lastWeekMonth < 10) {
	    lastWeekMonth = "0" + lastWeekMonth;
	}
	if(lastWeekDate < 10) {
	    lastWeekDate = "0" + lastWeekDate;
	}
	

    if(n == 5) {
	    return lastWeekMonth + "-" + lastWeekDate;
	}
    return lastWeekYear + "-" + lastWeekMonth + "-" + lastWeekDate;
}

 

5.js格式化函数,是完全在客户端获取Date对象后的格式化

Date.prototype.format = function(format) {
    var o =
    {
        "M+" : this.getMonth()+1, //month
        "d+" : this.getDate(),    //day
        "h+" : this.getHours(),   //hour
        "m+" : this.getMinutes(), //minute
        "s+" : this.getSeconds(), //second
        "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
        "S" : this.getMilliseconds() //millisecond
    }
    if(/(y+)/.test(format))
    format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
    for(var k in o)
    if(new RegExp("("+ k +")").test(format))
    format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
    return format;
}

var ddd = new Date();
document.write (ddd.format('yyyy-MM-dd hh:mm:ss<br>'));
document.write (ddd.format('yyyy-MM-dd'));

 

6.

 

你可能感兴趣的:(jquery,算法,Ajax,IE,prototype)