作者:jzcpojwmds_652 | 来源:互联网 | 2023-07-20 13:24
JS—通讯1、挪动端打电话window.location.href(tel:+phone);2、挪动端发送短信–Android、iOSvarunavigator.userAgent
JS — 通讯
1、挪动端打电话
window.location.href = ("tel:" + phone);
2、挪动端发送短信–Android、iOS
var u = navigator.userAgent, app = navigator.appVersion;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; // android终端或许uc浏览器
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
//sms:10086?body=1008611 sms:10086&body=1008611
if(isAndroid == true) {
window.location.href=("sms:10694006929598?body="+text);
} else if(isiOS == true) {
window.location.href=("sms:10694006929598&body="+text);
}
JS — 正则
手机号码
/^1(3[0-9]|4[57]|5[0-35-9]|7[0135678]|8[0-9])\d{8}$/
电子邮箱
/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
/^[a-z\d]+(\.[a-z\d]+)*@([\da-z](-[\da-z])?)+(\.{1,2}[a-z]+)+$/
删除 emoji 脸色
str.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g,'');
- 猎取用户地舆位置
毛病代码:
-Permission denied – 用户不允许地舆定位
-Position unavailable – 没法猎取当前位置
-Timeout – 操纵超时
-Unknown error – 未知毛病
JS — 数组去重
Array.prototype.unique_filterArray = Array.prototype.unique_filterArray || function(){
return this.filter(function(item, index, arr){
return arr.indexOf(item) === index;
});
}
Array.prototype.unique = function(){
var res = [];
var json = {};
for(var i = 0; i if(!json[this[i]]){
res.push(this[i]);
json[this[i]] = 1;
}
}
return res;
};
arr.unique();//挪用 Array.prototype.unique
Array.prototype.remove = function(val) {
var index = this.indexOf(val);
if (index > -1) {
this.splice(index, 1);
}
};
运用JQ删除某一项 -- arr.splice($.inArray(item,arr),1);
JS — 剖析URI参数
*将GET参数根据键值对的情势输出json
var str = ‘http://item.taobao.com/item.h…‘;*
function getUrl(str) {
var data1=str.split("?")[1];
var result={};
if(data1.indexOf("&") > -1) {
var bigArr=data1.split("&");
for(var a=0,b=bigArr.length;a var smallArr=bigArr[a].split("=");
result[smallArr[0]]=smallArr[1];
}
} else {
var arr1=data1.split("=");
result[arr1[0]]=arr1[1];
}
console.log(result);
return result;
}
getUrl("http://10.8.15.176:666/wx.html?a=1");//{a:"1"}
getUrl("http://10.8.15.176:666/wx.html?a=1&b=2&c=3&d");//{a: "1", b: "2", c: "3", d: undefined}
剖析单个参数
function getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null;
}
http://10.8.15.176:666/bindSuccess/coupons.html?values=uri1&gets=uri2&types=uri3
var values=getQueryString("values") ||300,
gets=getQueryString("gets"),
types=getQueryString("types");
JS — 完成数组迭代要领
1、完成arr.forEach() IE8及以下不支持原生 Array.prototype.forEach
参考底部 Array.prototype.forEach
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(callback, thisArg) {
var T, k;
if (this == null) {
throw new TypeError('this is null or not defined');
}
var O = Object(this);
var len = O.length >>> 0;
// 一切非数值转换成0;一切大于即是 0 等数取整数部份
if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function');
}
if (arguments.length > 1) {
T = thisArg;
}
k = 0;
while (k var kValue;
if (k in O) {
kValue = O[k];
callback.call(T, kValue, k, O);
}
k++;
}
};
}
2、完成arr.filter()
参考底部 Array.prototype.filter
if (!Array.prototype.filter){
Array.prototype.filter = function(func, thisArg) {
'use strict';
if ( ! ((typeof func === 'Function' || typeof func === 'function') && this) )
throw new TypeError();
var len = this.length >>> 0,
res = new Array(len), // preallocate array
t = this, c = 0, i = -1;
if (thisArg === undefined){
while (++i !== len){
// checks to see if the key was set
if (i in this){
if (func(t[i], i, t)){
res[c++] = t[i];
}
}
}
}
else{
while (++i !== len){
// checks to see if the key was set
if (i in this){
if (func.call(thisArg, t[i], i, t)){
res[c++] = t[i];
}
}
}
}
res.length = c; // shrink down array to proper size
return res;
};
}
JS — COOKIE
var COOKIEUtil = {
get: function(name) {
var COOKIEName = encodeURIComponent(name) + "=",
COOKIEStart = document.COOKIE.indexOf(COOKIEName),
COOKIEValue = null;
if(COOKIEStart > -1) {
var COOKIEEnd = document.COOKIE.indexOf(';', COOKIEStart);
if(COOKIEEnd === -1) {
COOKIEEnd = document.COOKIE.length;
}
COOKIEValue = decodeURIComponent(document.COOKIE.substring(COOKIEStart+COOKIEName.length, COOKIEEnd));
}
return COOKIEValue;
},
set: function(name, value, expires, path, domain, secure) {
var COOKIEText = encodeURIComponent(name)+'='+encodeURIComponent(value);
if(expires instanceof Date) {
COOKIEText += '; expires=' + expires.toGMTString();
} else if(typeof expires === 'number') {
COOKIEText += '; expires=' + (new Date(expires*24*60*60*1000+Date.now())).toGMTString();
}
(new Date(毫秒数)).toGMTString()
7天后 (new Date(7*24*60*60*1000+Date.now())).toGMTString()
if(path) { COOKIEText += '; path=' + path; }
if(domain) { COOKIEText += '; domain=' + domain; }
if(secure) { COOKIEText += '; secure'; }
document.COOKIE = COOKIEText;
},
unset: function(name, path, domain, secure) { this.set(name, '', new Date(0), path, domain, secure); }
}
name: COOKIE唯一的称号 COOKIE必需经由URL编码 不辨别大小写 实践中最好看成COOKIE辨别大小写
value: 字符串值 必需经由URL编码
expires: 失效时候 COOKIE什么时候被删除的时候戳 默许情况下会话完毕立行将一切COOKIE删除
path: 域 一切向该域的要求中都邑包括COOKIE 能够包括子域名 也能够不包括
domain: 途径 关于指定域中的谁人途径 应该向服务器发送COOKIE
secure: 安全标志 指定后,COOKIE只要在运用SSL衔接时才会发送到服务器 是COOKIE中唯一一个非名值对的,直接包括secure