分享:一个大厂JS方法小案例

保留一个JS小代码段,备用。

<code>//域名( cookie 会用到) 

webDomain = "ckpay.baidu.com";

//ajax 超时时间 单位毫秒
global_AjaxTimeOut = 10000;

//实现 trim 方法
String.prototype.trim = function () {
if (this == null) {
return "";
}
return this.replace(/(^\\s*)|(\\s*$)/g, "");
};

//实现 replaceAll 方法
String.prototype.replaceAll = function (s1, s2) {
return this.replace(new RegExp(s1, "gm"), s2);
};

//得到radio的值
function getRadioValue(radioName) {
var objs = document.getElementsByName(radioName);
for (var i = 0; i < objs.length; i++) {
if (objs[i].checked) {
return objs[i].value;
}
}
}

//验证邮件是否合法
function emailCheck(mail) {
var emailPat = /^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$/;
var matchArray = mail.match(emailPat);
if (matchArray == null) {
return false;
}
return true;
}

/////////////////////////////////////////////////////////////////
// 在 cookie.js 基础上封装的几个 cookie 操作函数
function addTempCookie(cookieName, cookieValue) {
Set_Cookie(cookieName, cookieValue, 0, "/", webDomain, true);
}

function addCookie(cookieName, cookieValue, time) {
if (!time) {

time = 3600 * 24 * 365 * 30;
}
Set_Cookie(cookieName, cookieValue, time, "/", webDomain, true);
}

function delCookie(name) {
Delete_Cookie(name, "/", webDomain);
}

function getCookie(name) {
return Get_Cookie(name);
}

//发送 post get 信息
function getNewSubmitForm(theMethod) {
var submitForm = document.createElement("FORM");
document.body.appendChild(submitForm);
submitForm.method = theMethod;
return submitForm;
}

//helper function to add elements to the form
function createNewFormElement(inputForm, elementName, elementValue) {
var newElement = document.createElement("");
inputForm.appendChild(newElement);
newElement.value = elementValue;
return newElement;
}

//获得静态网页的网页参数
function getQueryString(name) {
var reg = new RegExp("(^|\\\\?|&)" + name + "=([^&]*)(\\\\s|&|$)", "i");
if (reg.test(window.location.href)) {
return unescape(RegExp.$2.replace(/\\+/g, " "));
} else {
return "";
}
}

//获得网页url 第一个 ? 后的字符串
function getQueryStr() {
var url = window.location.href;
var i = url.indexOf("?");
if (i > 0) {
return url.substring(i + 1, url.length);
} else {
return "";
}

}

/////////////////////////
//动态加载 js文件
function IncludeJS(sId, fileUrl, source) {
if ((source != null) && (!document.getElementById(sId))) {
var oHead = document.getElementsByTagName("HEAD").item(0);
var oScript = document.createElement("script");
oScript.language = "javascript";
oScript.type = "text/javascript";
oScript.id = sId;
oScript.defer = true;
oScript.text = source;
oHead.appendChild(oScript);
}
}

//添加一个隐藏的 frame 并访问 url
function hiddenFrameAction(id, url) {
var iframe = document.getElementById(id);
if (iframe == null) {
iframe = document.createElement("<iframe>");
document.body.appendChild(iframe);
}
iframe.src = url;
}

//兼容多种浏览器的Ctrl+Enter提交--兼容firefox、ie、opera
function isKeyTrigger(e, keyCode) {
var argv = isKeyTrigger.arguments;
var argc = isKeyTrigger.arguments.length;
var bCtrl = false;
if (argc > 2) {
bCtrl = argv[2];
}
var bAlt = false;
if (argc > 3) {
bAlt = argv[3];
}
var nav4 = window.Event ? true : false;
if (typeof e == "undefined") {
e = event;
}
if (bCtrl && !((typeof e.ctrlKey != "undefined") ? e.ctrlKey : e.modifiers & Event.CONTROL_MASK > 0)) {
return false;
}
if (bAlt && !((typeof e.altKey != "undefined") ? e.altKey : e.modifiers & Event.ALT_MASK > 0)) {

return false;
}
var whichCode = 0;
if (nav4) {
whichCode = e.which;
} else {
if (e.type == "keypress" || e.type == "keydown") {
whichCode = e.keyCode;
} else {
whichCode = e.button;
}
}
return (whichCode == keyCode);
}

//清除字段内容
function cleanValue(id) {
var obj = $("#" + id);
obj.val("");
}

function setValue(id, value) {
var obj = $("#" + id);
obj.val(value);
}

//=============================
/*
* ForDight(Dight,How):数值格式化函数,Dight要
* 格式化的 数字,How要保留的小数位数。
*/
function ForDight(Dight, How) {
Dight = Math.round(Dight * Math.pow(10, How)) / Math.pow(10, How);
return Dight;
}

//=============================


//显示出过度效果的图标
function showUnloadGif(timeOut) {
var unloadGif = $("#unloadGif");
if (unloadGif.size() == 0) {
$('
').appendTo("body");
}
unloadGif.css("display", "block");
if (timeOut == null) {
timeOut = 5000;//5秒钟后自然也要消失
}
window.setTimeout("hideUnloadGif()", timeOut);
}

//隐藏过渡效果的图标
function hideUnloadGif() {
$("#unloadGif").hide();
}

//input 选择的和没选择的效果变化
function inputCss(inputId) {
$("#" + inputId).focus(function () {
$("#" + inputId).parent().addClass("focus");
});
$("#" + inputId).blur(function () {
$("#" + inputId).parent().removeClass("focus");
});
}

function quanjiao2banjiao(str) {
var flag = false;
var result = '';
for (i = 0; i < str.length; i++) {
code = str.charCodeAt(i);
if (code >= 65281 && code <= 65373) {
flag = true;
result += String.fromCharCode(str.charCodeAt(i) - 65248);
} else if (code == 12288) {//space
flag = true;
result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32);
} else {
result += str.charAt(i);
}
}
if (flag == true) {
return result;
} else {
return "";
}
}

function quanjiaoOndo(obj) {

var str = quanjiao2banjiao(obj.value);
if (str != "") {
obj.value = str;
}
}

//输入功能辅助,给出input的默认提示信息,当鼠标点击后自动消失
function inputHelp(id, tip) {
if ($("#" + id).val() == "") {
$("#" + id).val(tip);
} else {//默认颜色问题处理
$("#" + id).attr("style", "color:black;");
}
$("#" + id).mousedown(function () {
if ($("#" + id).val() == tip) {
$("#" + id).val("");
$("#" + id).attr("style", "color:black;");
}
});
$("#" + id).select(function () {
if ($("#" + id).val() == tip) {
$("#" + id).val("");
$("#" + id).attr("style", "color:black;");
}
});
$("#" + id).blur(function () {
if ($("#" + id).val() == "") {
$("#" + id).val(tip);
$("#" + id).attr("style", "color:#999;");
}
});
}
/<code>

目的:防止丢失,随时来头条取。

分享:一个大厂JS方法小案例


分享到:


相關文章: