JS验证控件1.3
更新内容:1.新增可以自定义提示参数名
2.提交等事件可以只绑定一次
3.整合一些常用的验证到里面去
var urlHelp = {};
// 跳转页面
urlHelp.GoUrl = function(url, addbackurl) {
if (addbackurl != true)
addbackurl = false;
if (addbackurl == true) {
var backurl = urlHelp.request('backurl');
if (backurl != null && backurl != undefined && backurl != "") {
url = urlHelp.setRequest(url, "backurl", backurl);
}
}
location.replace(url);
}
//得到当前页面URL
urlHelp.getPageUrl = function() {
var oldurl = location.href;
var url = oldurl.substring(oldurl.length - 1, oldurl.length);
while (url == "#") {
oldurl = oldurl.substring(0, oldurl.length - 1);
url = oldurl.substring(oldurl.length - 1, oldurl.length);
}
oldurl = urlHelp.setRequest(oldurl, "mathrandom", Math.random());
return oldurl;
if (oldurl.indexOf('?') != -1) {
var index = oldurl.lastIndexOf("&", oldurl.length);
if (index != -1) {
var a = oldurl.substring(index + 1, oldurl.length);
if (!isNaN(a)) {
oldurl = oldurl.substring(0, index)
}
}
oldurl = oldurl + "&" + Math.random();
}
else {
oldurl = oldurl + "?" + Math.random();
}
return oldurl;
}
//对内容进行编码
urlHelp.getEncodeURI = function(content) {
content = encodeURIComponent(content);
content = urlHelp.MyReplace(content, "#", "%23");
return content;
}
// 对内容进行替换
urlHelp.MyReplace = function(content, value1, value2) {
var reg = new RegExp(value1, "g");
content = content.replace(reg, value2);
return content;
}
// 获取URL参数
urlHelp.request = function(paras) {
var url = location.href;
var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
var paraObj = {}
for (i = 0; j = paraString[i]; i++) {
paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length);
}
var returnValue = paraObj[paras.toLowerCase()];
if (typeof (returnValue) == "undefined") {
return "";
} else {
return urlHelp.MyReplace(returnValue, '#', '');
}
}
urlHelp.setRequest = function(url, par, par_value) {
var pattern = par + '=([^&]*)';
var replaceText = par + '=' + par_value;
if (url.match(pattern)) {
var tmp = '/\\' + par + '=[^&]*/';
tmp = url.replace(eval(tmp), replaceText);
return (tmp);
}
else {
if (url.match('[\?]')) {
return url + '&' + replaceText;
}
else {
return url + '?' + replaceText;
}
}
return url;
}
// JavaScript Document
if (typeof (HTMLElement) != "undefined") {
HTMLElement.prototype.insertAdjacentElement = function(where, parsedNode) {
switch (where) {
case "beforeBegin":
this.parentNode.insertBefore(parsedNode, this);
break;
case "afterBegin":
this.insertBefore(parsedNode, this.firstChild);
break;
case "beforeEnd":
this.appendChild(parsedNode);
break;
case "afterEnd":
if (this.nextSibling)
this.parentNode.insertBefore(parsedNode, this.nextSibling);
else
this.parentNode.appendChild(parsedNode);
break;
}
}
HTMLElement.prototype.insertAdjacentHTML = function(where, htmlStr) {
var r = this.ownerDocument.createRange();
r.setStartBefore(this);
var parsedHTML = r.createContextualFragment(htmlStr);
this.insertAdjacentElement(where, parsedHTML);
}
HTMLElement.prototype.insertAdjacentText = function(where, txtStr) {
var parsedText = document.createTextNode(txtStr);
this.insertAdjacentElement(where, parsedText);
}
}
function addEvent(element, type, handler, funname) {
if (!element) {
return;
}
//为每一个事件处理函数分派一个唯一的ID
if (!handler.$$guid) handler.$$guid = addEvent.guid++;
try {
if (!funname) funname = handler.$$guid;
if (!element.funnames) element.setAttribute('funnames', "");
if (element.funnames.split(',').Any(funname)) {
return;
}
} catch (e) { }
//为元素的事件类型创建一个哈希表
if (!element.events) element.events = {};
//为每一个"元素/事件"对创建一个事件处理程序的哈希表
var handlers = element.events[type];
if (!handlers) {
handlers = element.events[type] = {};
//存储存在的事件处理函数(如果有)
if (element["on" + type]) {
handlers[0] = element["on" + type];
}
}
//将事件处理函数存入哈希表
handlers[handler.$$guid] = handler;
//指派一个全局的事件处理函数来做所有的工作
element["on" + type] = handleEvent;
try {
element.setAttribute('funnames', element.funnames + funname + ",");
} catch (e) { }
};
//用来创建唯一的ID的计数器
addEvent.guid = 1;
function removeAllEvent(element, type) {
if (element && element.events && element.events != null) {
element.events[type] = {};
}
}
function removeEvent(element, type, handler) {
//从哈希表中删除事件处理函数 www.zzzyk.com
if (element && element.events && element.events[type]) {
delete element.events[type][handler.$$guid];
}
};
function handleEvent(event) {
var returnValue = true;
try {
//抓获事件对象(IE使用全局事件对象)
event = event || fixEvent(window.event);
//取得事件处理函数的哈希表的引用
var handlers = this.events[event.type];
//执行每一个处理函数
for (var i in handlers) {
this.$$handleEvent = handlers[i];
if (this.$$handleEvent(event) === false) {
returnValue = false;
}
}
} catch (e) { }
return returnValue;
};
//为IE的事件对象添加一些“缺失的”函数
function fixEvent(event) {
if (event != null) {
//添加标准的W3C方法
event.preventDefault = fixEvent.preventDefault;
event.stopPropagation = fixEvent.stopPropagation;
}
return event;
};
fixEvent.preventDefault = function() {
this.returnValue = false;
};
fixEvent.stopPropagation = function() {
this.cancelBubble = true;
};
Array.prototype.S = String.fromCharCode(2);
Array.prototype.Any = function(e) {
var r = new RegExp(this.S + e + this.S);
return (r.test(this.S + this.join(this.S) + this.S));
};
function addEvent(element, type, handler, funname) {
//为每一个事件处理函数分派一个唯一的ID
if (!handler.$$guid) handler.$$guid = addEvent.guid++;
try {
if (!funname) funname = handler.$$guid;
var funnames = element.getAttribute("funnames");
if (funnames == null || funnames == "") { element.setAttribute("funnames"补充:web前端 , JavaScript ,
- 更多JS疑问解答:
- 几个验证11位手机号码格式的js代码
- js把图片转换成 base64代码
- js把base代码转换成图片
- JS 将 base64编码的图片转化为图片文件
- js中的定时器
- js如何获得FCKeditor控件的值
- 用js限制投票的cookie .目前设置的为:<input type="" class="" onclick="'window.location...
- JS验证,这块“牛皮”反复修改都不能实现
- 在JS中使用DOM模型
- 如何用JS 获取本地文件夹的文件列表
- js中new 了两个Object数组。怎么样将数组内容合并,重复的内容?
- 求实现自动生成图片缩略图的JS代码
- JS脚本网页问题
- js,代码中"object"和"Object"区别?
- js+flash实现网页图片切换效果,出现边框,单击激活此控件。