function isDigit(value) {
var patrn = /^[0-9]*$/;
if (patrn.exec(value) == null || value == "") {
return false
} else {
return true
}
}
9、原生JavaScript设置cookie值
function setCookie(name, value, Hours) {
var d = new Date();
var offset = 8;
var utc = d.getTime() + (d.getTimezoneOffset() * 60000);
var nd = utc + (3600000 * offset);
var exp = new Date(nd);
exp.setTime(exp.getTime() + Hours * 60 * 60 * 1000);
document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString() + ";domain=360doc.com;"
}
10、原生JavaScript获取cookie值
function getCookie(name) {
var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
if (arr != null) return unescape(arr[2]);
return null
}
function $(id) {
return !id ? null : document.getElementById(id);
}
19、原生JavaScript返回浏览器版本内容
function browserVersion(types) {
var other = 1;
for(i in types) {
var v = types[i] ? types[i] : i;
if(USERAGENT.indexOf(v) != -1) {
var re = new RegExp(v + '(\\/|\\s)([\\d\\.]+)', 'ig');
var matches = re.exec(USERAGENT);
var ver = matches != null ? matches[2] : 0;
other = ver !== 0 && v != 'mozilla' ? 0 : other;
}else {
var ver = 0;
}
eval('BROWSER.' + i + '= ver');
}
BROWSER.other = other;
}
function insertAfter(newChild,refChild){
var parElem=refChild.parentNode;
if(parElem.lastChild==refChild){
refChild.appendChild(newChild);
}else{
parElem.insertBefore(newChild,refChild.nextSibling);
}
}
22、原生JavaScript中兼容浏览器绑定元素事件
function addEventSamp(obj,evt,fn){
if (obj.addEventListener) {
obj.addEventListener(evt, fn, false);
}else if(obj.attachEvent){
obj.attachEvent('on'+evt,fn);
}
}
23、原生JavaScript光标停在文字的后面,文本框获得焦点时调用
function focusLast(){
var e = event.srcElement;
var r =e.createTextRange();
r.moveStart('character',e.value.length);
r.collapse(true);
r.select();
}
24、原生JavaScript检验URL链接是否有效
function getUrlState(URL){
var xmlhttp = new ActiveXObject("microsoft.xmlhttp");
xmlhttp.Open("GET",URL, false);
try{
xmlhttp.Send();
}catch(e){
}finally{
var result = xmlhttp.responseText;
if(result){
if(xmlhttp.Status==200){
return(true);
}else{
return(false);
}
}else{
return(false);
}
}
}
25、原生JavaScript格式化CSS样式代码
function formatCss(s){//格式化代码
s = s.replace(/\s*([\{\}\:\;\,])\s*/g, "$1");
s = s.replace(/;\s*;/g, ";"); //清除连续分号
s = s.replace(/\,[\s\.\#\d]*{/g, "{");
s = s.replace(/([^\s])\{([^\s])/g, "$1 {\n\t$2");
s = s.replace(/([^\s])\}([^\n]*)/g, "$1\n}\n$2");
s = s.replace(/([^\s]);([^\s\}])/g, "$1;\n\t$2");
return s;
}
26、原生JavaScript压缩CSS样式代码
function yasuoCss (s) {//压缩代码
s = s.replace(/\/\*(.|\n)*?\*\//g, ""); //删除注释
s = s.replace(/\s*([\{\}\:\;\,])\s*/g, "$1");
s = s.replace(/\,[\s\.\#\d]*\{/g, "{"); //容错处理
s = s.replace(/;\s*;/g, ";"); //清除连续分号
s = s.match(/^\s*(\S+(\s+\S+)*)\s*$/); //去掉首尾空白
return (s == null) ? "" : s[1];
}
27、原生JavaScript获取当前路径
var currentPageUrl = "";
if (typeof this.href === "undefined") {
currentPageUrl = document.location.toString().toLowerCase();
}
else {
currentPageUrl = this.href.toString().toLowerCase();
}
28、原生JavaScriptIP转成整型
function _ip2int(ip){
var num = 0;
ip = ip.split(".");
num = Number(ip[0]) * 256 * 256 * 256 + Number(ip[1]) * 256 * 256 + Number(ip[2]) * 256 + Number(ip[3]);
num = num >>> 0;
return num;
}
function checkAll() {
var selectall = document.getElementById("selectall");
var allbox = document.getElementsByName("allbox");
if (selectall.checked) {
for (var i = 0; i < allbox.length; i++) {
allbox[i].checked = true;
}
} else {
for (var i = 0; i < allbox.length; i++) {
allbox[i].checked = false;
}
}
}
31、原生JavaScript判断是否移动设备
function isMobile(){
if (typeof this._isMobile === 'boolean'){
return this._isMobile;
}
var screenWidth = this.getScreenWidth();
var fixViewPortsExperiment = rendererModel.runningExperiments.FixViewport || rendererModel.runningExperiments.fixviewport;
var fixViewPortsExperimentRunning = fixViewPortsExperiment && (fixViewPortsExperiment.toLowerCase() === "new");
if(!fixViewPortsExperiment){
if(!this.isAppleMobileDevice()){
screenWidth = screenWidth/window.devicePixelRatio;
}
}
var isMobileScreenSize = screenWidth < 600;
var isMobileUserAgent = false;
this._isMobile = isMobileScreenSize && this.isTouchScreen();
return this._isMobile;
}
32、原生JavaScript判断是否移动设备访问
function isMobileUserAgent(){
return (/iphone|ipod|android.*mobile|windows.*phone|blackberry.*mobile/i.test(window.navigator.userAgent.toLowerCase()));
}
33、原生JavaScript判断是否苹果移动设备访问
function isAppleMobileDevice(){
return (/iphone|ipod|ipad|Macintosh/i.test(navigator.userAgent.toLowerCase()));
}
34、原生JavaScript判断是否安卓移动设备访问
function isAndroidMobileDevice(){
return (/android/i.test(navigator.userAgent.toLowerCase()));
}
35、原生JavaScript判断是否Touch屏幕
function isTouchScreen(){
return (('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch);
}
36、原生JavaScript判断是否在安卓上的谷歌浏览器
function isNewChromeOnAndroid(){
if(this.isAndroidMobileDevice()){
var userAgent = navigator.userAgent.toLowerCase();
if((/chrome/i.test(userAgent))){
var parts = userAgent.split('chrome/');
var fullVersionString = parts[1].split(" ")[0];
var versionString = fullVersionString.split('.')[0];
var version = parseInt(versionString);
if(version >= 27){
return true;
}
}
}
return false;
}
37、原生JavaScript判断是否打开视窗
function isViewportOpen() {
return !!document.getElementById('wixMobileViewport');
}
function getScreenWidth(){
var smallerSide = Math.min(screen.width, screen.height);
var fixViewPortsExperiment = rendererModel.runningExperiments.FixViewport || rendererModel.runningExperiments.fixviewport;
var fixViewPortsExperimentRunning = fixViewPortsExperiment && (fixViewPortsExperiment.toLowerCase() === "new");
if(fixViewPortsExperiment){
if(this.isAndroidMobileDevice() && !this.isNewChromeOnAndroid()){
smallerSide = smallerSide/window.devicePixelRatio;
}
}
return smallerSide;
}
41、原生JavaScript完美判断是否为网址
function IsURL(strUrl) {
var regular = /^\b(((https?|ftp):\/\/)?[-a-z0-9]+(\.[-a-z0-9]+)*\.(?:com|edu|gov|int|mil|net|org|biz|info|name|museum|asia|coop|aero|[a-z][a-z]|((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]\d)|\d))\b(\/[-a-z0-9_:\@&?=+,.!\/~%\$]*)?)$/i
if (regular.test(strUrl)) {
return true;
}
else {
return false;
}
}
42、原生JavaScript根据样式名称检索元素对象
function getElementsByClassName(name) {
var tags = document.getElementsByTagName('*') || document.all;
var els = [];
for (var i = 0; i < tags.length; i++) {
if (tags[i].className) {
var cs = tags[i].className.split(' ');
for (var j = 0; j < cs.length; j++) {
if (name == cs[j]) {
els.push(tags[i]);
break
}
}
}
}
return els
}
43、原生JavaScript判断是否以某个字符串开头
String.prototype.startWith = function (s) {
return this.indexOf(s) == 0
}
44、原生JavaScript判断是否以某个字符串结束
String.prototype.endWith = function (s) {
var d = this.length - s.length;
return (d >= 0 && this.lastIndexOf(s) == d)
}
45、原生JavaScript返回IE浏览器的版本号
高版IE支持 xhr , window.ActiveXObject 会返回 undefined
function getIE(){
if (window.ActiveXObject){
var v = navigator.userAgent.match(/MSIE ([^;]+)/)[1];
return parseFloat(v.substring(0, v.indexOf(".")))
}
return false
}
46、原生JavaScript获取页面高度
function getPageHeight(){
var g = document, a = g.body, f = g.documentElement, d = g.compatMode == "BackCompat"
? a
: g.documentElement;
return Math.max(f.scrollHeight, a.scrollHeight, d.clientHeight);
}
47、原生JavaScript获取页面scrollLeft
function getPageScrollLeft(){
var a = document;
return a.documentElement.scrollLeft || a.body.scrollLeft;
}
48、原生JavaScript获取页面可视宽度
function getPageViewWidth(){
var d = document, a = d.compatMode == "BackCompat"
? d.body
: d.documentElement;
return a.clientWidth;
}
49、原生JavaScript获取页面宽度
function getPageWidth(){
var g = document, a = g.body, f = g.documentElement, d = g.compatMode == "BackCompat"
? a
: g.documentElement;
return Math.max(f.scrollWidth, a.scrollWidth, d.clientWidth);
}
50、原生JavaScript获取页面scrollTop
function getPageScrollTop(){
var a = document;
return a.documentElement.scrollTop || a.body.scrollTop;
}
51、原生JavaScript获取页面可视高度
function getPageViewHeight() {
var d = document, a = d.compatMode == "BackCompat"
? d.body
: d.documentElement;
return a.clientHeight;
}
function checkall(form, prefix, checkall) {
var checkall = checkall ? checkall : 'chkall';
for(var i = 0; i < form.elements.length; i++) {
var e = form.elements[i];
if(e.type=="checkbox"){
e.checked = form.elements[checkall].checked;
}
}
}
66、原生JavaScript实现全部取消选择通用方法
function uncheckAll(form) {
for (var i=0;i<form.elements.length;i++){
var e = form.elements[i];
if (e.name != 'chkall')
e.checked=!e.checked;
}
}
67、原生JavaScript实现打开一个窗体通用方法
function openWindow(url,windowName,width,height){
var x = parseInt(screen.width / 2.0) - (width / 2.0);
var y = parseInt(screen.height / 2.0) - (height / 2.0);
var isMSIE= (navigator.appName == "Microsoft Internet Explorer");
if (isMSIE) {
var p = "resizable=1,location=no,scrollbars=no,width=";
p = p+width;
p = p+",height=";
p = p+height;
p = p+",left=";
p = p+x;
p = p+",top=";
p = p+y;
retval = window.open(url, windowName, p);
} else {
var win = window.open(url, "ZyiisPopup", "top=" + y + ",left=" + x + ",scrollbars=" + scrollbars + ",dialog=yes,modal=yes,width=" + width + ",height=" + height + ",resizable=no" );
eval("try { win.resizeTo(width, height); } catch(e) ");
win.focus();
}
}
68、原生JavaScript判断是否为客户端设备
function client(o){
var b = navigator.userAgent.toLowerCase();
var t = false;
if (o == 'isOP'){
t = b.indexOf('opera') > -1;
}
if (o == 'isIE'){
t = b.indexOf('msie') > -1;
}
if (o == 'isFF'){
t = b.indexOf('firefox') > -1;
}
return t;
}
function isLetters( str ){
var re=/^[A-Za-z]+$/;
if (str.match(re) == null)
return false;
else
return true;
}
79、原生JavaScript判断字符串是否邮政编码
function isValidPost( chars ) {
var re=/^\d{6}$/;
if (chars.match(re) == null)
return false;
else
return true;
}
80、原生JavaScript判断字符是否空NULL
function isNULL( chars ) {
if (chars == null)
return true;
if (jsTrim(chars).length==0)
return true;
return false;
}
81、原生JavaScript用正则表达式提取页面代码中所有网址
var aa = document.documentElement.outerHTML.match(/(url\(|src=|href=)[\"\']*([^\"\'\(\)\<\>\[\] ]+)[\"\'\)]*|(http:\/\/[\w\-\.]+[^\"\'\(\)\<\>\[\] ]+)/ig).join("\r\n").replace(/^(src=|href=|url\()[\"\']*|[\"\'\>\) ]*$/igm,"");
alert(aa)
String.prototype.unique=function(){
var x=this.split(/[\r\n]+/);
var y='';
for(var i=0;i<x.length;i++){
if(!new RegExp("^"+x[i].replace(/([^\w])/ig,"\\$1")+"$","igm").test(y)){
y+=x[i]+"\r\n"
}
}
return y
}
84、原生JavaScript用正则表达式按字母排序,对每行进行数组排序
function SetSort(){
var text=K1.value.split(/[\r\n]/).sort().join("\r\n");//顺序
var test=K1.value.split(/[\r\n]/).sort().reverse().join("\r\n");//反序
K1.value=K1.value!=text?text:test;
}
85、原生JavaScript字符串反序
function IsReverse(text){
return text.split('').reverse().join('');
}
86、原生JavaScript用正则表达式清除html代码中的脚本
function clear_script(){
K1.value=K1.value.replace(/<script.*?>[\s\S]*?<\/script>|\s+on[a-zA-Z]{3,16}\s?=\s?"[\s\S]*?"|\s+on[a-zA-Z]{3,16}\s?=\s?'[\s\S]*?'|\s+on[a-zA-Z]{3,16}\s?=[^ >]+/ig,"");
}
87、原生JavaScript动态执行JavaScript脚本
function javascript(){
try{
eval(K1.value);
}catch(e){
alert(e.message);
}
}
88、原生JavaScript动态执行VBScript脚本
function vbscript(){
try{
var script=document.getElementById("K1").value;
if(script.trim()=="")return;
window.execScript('On Error Resume Next \n'+script+'\n If Err.Number<>0 Then \n MsgBox "请输入正确的VBScript脚本!",48,"脚本错误!" \n End If',"vbscript")
}catch(e){
alert(e.message);
}
}
89、原生JavaScript实现金额大写转换函数
function transform(tranvalue) {
try {
var i = 1;
var dw2 = new Array("", "万", "亿"); //大单位
var dw1 = new Array("拾", "佰", "仟"); //小单位
var dw = new Array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"); //整数部分用
//以下是小写转换成大写显示在合计大写的文本框中
//分离整数与小数
var source = splits(tranvalue);
var num = source[0];
var dig = source[1];
//转换整数部分
var k1 = 0; //计小单位
var k2 = 0; //计大单位
var sum = 0;
var str = "";
var len = source[0].length; //整数的长度
for (i = 1; i <= len; i++) {
var n = source[0].charAt(len - i); //取得某个位数上的数字
var bn = 0;
if (len - i - 1 >= 0) {
bn = source[0].charAt(len - i - 1); //取得某个位数前一位上的数字
}
sum = sum + Number(n);
if (sum != 0) {
str = dw[Number(n)].concat(str); //取得该数字对应的大写数字,并插入到str字符串的前面
if (n == '0') sum = 0;
}
if (len - i - 1 >= 0) { //在数字范围内
if (k1 != 3) { //加小单位
if (bn != 0) {
str = dw1[k1].concat(str);
}
k1++;
} else { //不加小单位,加大单位
k1 = 0;
var temp = str.charAt(0);
if (temp == "万" || temp == "亿") //若大单位前没有数字则舍去大单位
str = str.substr(1, str.length - 1);
str = dw2[k2].concat(str);
sum = 0;
}
}
if (k1 == 3) //小单位到千则大单位进一
{
k2++;
}
}
//转换小数部分
var strdig = "";
if (dig != "") {
var n = dig.charAt(0);
if (n != 0) {
strdig += dw[Number(n)] + "角"; //加数字
}
var n = dig.charAt(1);
if (n != 0) {
strdig += dw[Number(n)] + "分"; //加数字
}
}
str += "元" + strdig;
} catch(e) {
return "0元";
}
return str;
}
//拆分整数与小数
function splits(tranvalue) {
var value = new Array('', '');
temp = tranvalue.split(".");
for (var i = 0; i < temp.length; i++) {
value[i] = temp[i];
}
return value;
}
function getViewSize(){
var de=document.documentElement;
var db=document.body;
var viewW=de.clientWidth==0 ? db.clientWidth : de.clientWidth;
var viewH=de.clientHeight==0 ? db.clientHeight : de.clientHeight;
return Array(viewW ,viewH);
}
97、原生JavaScript判断IE版本号(既简洁、又向后兼容!)
var _IE = (function(){
var v = 3, div = document.createElement('div'), all = div.getElementsByTagName('i');
while (
div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
all[0]
);
return v > 4 ? v : false ;
}());
98、原生JavaScript获取浏览器版本号
function browserVersion(types) {
var other = 1;
for (i in types) {
var v = types[i] ? types[i] : i;
if (USERAGENT.indexOf(v) != -1) {
var re = new RegExp(v + '(\\/|\\s|:)([\\d\\.]+)', 'ig');
var matches = re.exec(USERAGENT);
var ver = matches != null ? matches[2] : 0;
other = ver !== 0 && v != 'mozilla' ? 0 : other;
} else {
var ver = 0;
}
eval('BROWSER.' + i + '= ver');
}
BROWSER.other = other;
}
99、原生JavaScript半角转换为全角函数
function ToDBC(str){
var result = '';
for(var i=0; i < str.length; i++){
code = str.charCodeAt(i);
if(code >= 33 && code <= 126){
result += String.fromCharCode(str.charCodeAt(i) + 65248);
}else if (code == 32){
result += String.fromCharCode(str.charCodeAt(i) + 12288 - 32);
}else{
result += str.charAt(i);
}
}
return result;
}
100、原生JavaScript全角转换为半角函数
function ToCDB(str){
var result = '';
for(var i=0; i < str.length; i++){
code = str.charCodeAt(i);
if(code >= 65281 && code <= 65374){
result += String.fromCharCode(str.charCodeAt(i) - 65248);
}else if (code == 12288){
result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32);
}else{
result += str.charAt(i);
}
}
return result;
}
好长好长,总结的太多啦
转载的。。。。。
你肿么把出处给干掉了?
。。来源于网络。。。我无耻了。。