ITeye博客

js tools

xkh5566 · 2012-10-31 10:25 · Jquery · Web前端 · Markdown

// temp Id generator

SEQUENCE = {

value : -1,

next : function() {

this.value = ++this.value;

return this.value;

};

 

 

//reset form element

function clearForm(formName) {

    var formObj = document.forms[formName];

    var formEl = formObj.elements;

    for (var i=0; i<formEl.length; i++)

    {

        var element = formEl[i];

        if (element.type == 'submit') { continue; }

        if (element.type == 'reset') { continue; }

        if (element.type == 'button') { continue; }

        if (element.type == 'hidden') { continue; }

        if (element.type == 'text') { element.value = ''; }

        if (element.type == 'textarea') { element.value = ''; }

        if (element.type == 'checkbox') { element.checked = false; }

        if (element.type == 'radio') { element.checked = false; }

        if (element.type == 'select-multiple') { element.selectedIndex = -1; }

        if (element.type == 'select-one') { element.selectedIndex = -1; }

    }

}

 

//define stringBuffer object in present project

function StringBuffer() {

this.__strings__ = new Array;

}

 

StringBuffer.prototype.append = function(str) {

this.__strings__.push(str);

return this;

};

 

StringBuffer.prototype.toString = function() {

return this.__strings__.join("");

};

 

//extend one method,'startWith' for original string object

if(!String.prototype.startWith){

String.prototype.startWith = function(param){

if(!param){

return false;

}else{

if(String(this).substring(0,String(param).length)==param){

return true;

}else{

return false;

}

}

};

}

 

/**

 * 

 * @param divId  one container

 * @param info operation result message to show in the up DIV, such as: "Add Successfully!"

 * @param isSuccess one sign whether operate successfully.Just two values:true or false.true means success.

 * @returns undefined

 */

function showOperationResultInfo(divId,info,isSuccess){

$("#"+divId).removeAttr("style");

if(isSuccess){

$("#"+divId).attr("style","color:green");

}else{

$("#"+divId).attr("style","color:red");

}

$("#"+divId).empty();

$("#"+divId).html(info);

}

 

//refresh current page after ajax request

function deleteItemById(link){

$.ajax({

url :link,

cache:false,

context:document.body,

success:function(data){

if(!data.startWith("error")){

location.href="http://<%=request.getServerName()%>:<%=request.getServerPort()%><%=request.getContextPath() %>/demoWebSite/generateReport.action";

}else{

showOperationResultInfo("errorInfo",Messages.deleteFailureMess+",cause:"+data,false);

return false;

}

}

});

}

 

//obtain java version in the local PC by the plugin 'deployJava.js'

<script src="${contextPath}/js/plugins/deployJava.js"></script>

<script type="text/javascript">

var versions = deployJava.getJREs();

var reg = /1.6.|1.7.|1.8.|1.9./;

var result =  reg.exec(versions);

if(null == result){

$("#appletUploadControl").innerHTML = "Your java version is: <b>" + versions + "</b>, please install java1.6 or upper version<br/>" + "You could download latest JDK from here:" + "<a href='http://www.oracle.com/technetwork/java/javase/downloads/index.html', target='_blank'>oracle-java</a>";

}

</script>

/*

DESC:extend one method,'Format' from original Date object

for example: 

1. new Date().Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 ;

2. new Date().Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 ;

3. $("#createDateHtmlId").val(new Date().Format("MM/dd/yyyy")) .

*/

Date.prototype.Format = function(fmt) {

var o = {

"M+" : this.getMonth() + 1,

"d+" : this.getDate(), 

"h+" : this.getHours(),

"m+" : this.getMinutes(), 

"s+" : this.getSeconds(), 

"q+" : Math.floor((this.getMonth() + 3) / 3), 

"S" : this.getMilliseconds()

};

if (/(y+)/.test(fmt))

fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")

.substr(4 - RegExp.$1.length));

for ( var k in o)

if (new RegExp("(" + k + ")").test(fmt))

fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k])

: (("00" + o[k]).substr(("" + o[k]).length)));

return fmt;

};

<script type="text/javascript" src="${contextPath}/js/json.js"></script>

var project = {

value:"",

newProject:function(){

var one={

"id":"",

"project":"",

"projectCode":""

};

return one;

},

toJson:function(data){

return JSON.stringify(data);//the 'stringify' method from the plugin 'json.js'

},

};

//Construct one new instance

var item = project.newProject();

item.id=100;

item.project="demo";

item.projectCode="DE";

//Serialize object to character.After that,you can pass 'project.value' character as parameters for ajax request

project.value = project.toJson(item);

//transfer rgb to hexadecimal,such as (code section): var colorValue = rgb2hex($(this).parents("tr").css("color"));//"#ff6633"

function rgb2hex(rgb) {

if (!rgb) {

        return '#FFFFFF'; //default color

    }

    var hex_rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/); 

    function hex(x) {return ("0" + parseInt(x).toString(16)).slice(-2);}

    if (hex_rgb) {

        return "#" + hex(hex_rgb[1]) + hex(hex_rgb[2]) + hex(hex_rgb[3]);

    } else {

        return rgb; //ie8 returns background-color in hex format then it will make compatible, you can improve it checking if format is in hexadecimal

    }

}

//the use of ajax request to download file : <a href="javascript:void(0)"  onclick="return downloadFile(URL.generateReportReq);"></a>

function downloadFile(link){

var flagObj=false;

$.ajax({

url :link,

context:document.body,

success:function(data){

if(!data.startWith("error")){

flagObj = true;

} else{

alert(Messages.downloadFileMess);

flagObj =  false;

}

return;

}

});

return flagObj;

}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。