简单代码汇总
页面刷新(类似F5刷新): window.location.reload()
1.jQuery定时器
//html
<button id="btn">请发送验证码</button>
//js
var btn = document.getElementById('btn')
var a = true;
var len = 60;
btn.onclick = function() {
if (a) {
btn.innerHTML = len + 's';
var time = setInterval(function() {
btn.innerHTML = parseFloat(btn.innerHTML) - 1 + 's'
if (btn.innerHTML == '0s') {
clearInterval(time);
a = true;
}
}, 1000);
a = false;
} else {
return false;
}
}
2.判断是否是手机访问
//html
<p><label>js判断结果:</label><span id="rs_js" class="red"></span></p>
//js
if (/AppleWebKit.*Mobile/i.test(navigator.userAgent) || /Android/i.test(navigator.userAgent) || /BlackBerry/i.test(navigator.userAgent) || /IEMobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))) {
if (/iPad/i.test(navigator.userAgent)) {
$("#rs_js").text("iPad平板");
} else {
$("#rs_js").text("手机");
}
} else {
$("#rs_js").text("电脑");
}
3.判断是否是微信浏览器
function is_weixin() {
var ua = window.navigator.userAgent.toLowerCase();
if (ua.match(/MicroMessenger/i) == 'micromessenger') {
$("#rs").text("微信浏览器");
} else {
$("#rs").text("不是微信浏览器");
}
}is_weixin();
4.低版本IE判断
(function() {
if (navigator.userAgent.indexOf('MSIE') != -1) {
var ieBrowser = navigator.userAgent.split(';')[1];
if (ieBrowser.indexOf('MSIE') != -1 && parseInt(($.trim(ieBrowser)).substr(5)) < 9.0) {
window.location.href = 'browser_download.html';
}
}
}());
5.判断横屏还是竖屏
function orient() {
if (window.orientation == 0 || window.orientation == 180) {
// 竖屏
}else if (window.orientation == 90 || window.orientation == -90) {
//横屏
}
}
6.监测浏览器是Android的还是IOS的
var browser = {
versions: function() {
var u = navigator.userAgent;
return { //移动终端浏览器版本信息
trident: u.indexOf('Trident') > -1,
//IE内核
presto: u.indexOf('Presto') > -1,
//opera内核
webKit: u.indexOf('AppleWebKit') > -1,
//苹果、谷歌内核
gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1,
//火狐内核
mobile: !! u.match(/AppleWebKit.*Mobile.*/) || !! u.match(/AppleWebKit/),
//是否为移动终端
ios: !! u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),
//ios终端
android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1,
//android终端或者uc浏览器
iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1,
//是否为iPhone或者QQHD浏览器
iPad: u.indexOf('iPad') > -1,
//是否iPad
webApp: u.indexOf('Safari') == -1 //是否web应该程序,没有头部与底部
};
}(),
language: (navigator.browserLanguage || navigator.language).toLowerCase()
}
if (browser.versions.ios || browser.versions.iPhone || browser.versions.iPad) {
window.location = "https://itunes.apple.com/xxx";
} else if (browser.versions.android) {
window.location = "http://xxx/xxx.apk";
}
// document.writeln("语言版本: " + browser.language);
// document.writeln(" 是否为移动终端: " + browser.versions.mobile);
// document.writeln(" ios终端: " + browser.versions.ios);
// document.writeln(" android终端: " + browser.versions.android);
// document.writeln(" 是否为iPhone: " + browser.versions.iPhone);
// document.writeln(" 是否iPad: " + browser.versions.iPad);
// document.writeln(navigator.userAgent);
7.判断手机类型
window.onload = function () {
var u = navigator.userAgent;
if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) { //安卓手机
alert("安卓手机");
} else if (u.indexOf('iPhone') > -1) { //苹果手机
alert("苹果手机");
} else if (u.indexOf('Windows Phone') > -1) { //winphone手机
alert("winphone手机");
}
}
8.判断手机操作系统
function detectOS() {
var sUserAgent = navigator.userAgent;
var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows");
var isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC") || (navigator.platform == "Macintosh") || (navigator.platform == "MacIntel");
if (isMac) return "Mac";
var isUnix = (navigator.platform == "X11") && !isWin && !isMac;
if (isUnix) return "Unix";
var isLinux = (String(navigator.platform).indexOf("Linux") > -1);
if (isLinux) return "Linux";
if (isWin) {
var isWin2K = sUserAgent.indexOf("Windows NT 5.0") > -1 || sUserAgent.indexOf("Windows 2000") > -1;
if (isWin2K) return "Win2000";
var isWinXP = sUserAgent.indexOf("Windows NT 5.1") > -1 || sUserAgent.indexOf("Windows XP") > -1;
if (isWinXP) return "WinXP";
var isWin2003 = sUserAgent.indexOf("Windows NT 5.2") > -1 || sUserAgent.indexOf("Windows 2003") > -1;
if (isWin2003) return "Win2003";
var isWinVista= sUserAgent.indexOf("Windows NT 6.0") > -1 || sUserAgent.indexOf("Windows Vista") > -1;
if (isWinVista) return "WinVista";
var isWin7 = sUserAgent.indexOf("Windows NT 6.1") > -1 || sUserAgent.indexOf("Windows 7") > -1;
if (isWin7) return "Win7";
}
return "other";
}
document.writeln("您的操作系统是:" + detectOS());
9.判断APP浏览器(微信、微博、QQ、android、ios)
var ua = navigator.userAgent.toLowerCase();//获取判断用的对象
if (ua.match(/MicroMessenger/i) == "micromessenger") {
//在微信中打开
}
if (ua.match(/WeiBo/i) == "weibo") {
//在新浪微博客户端打开
}
if (ua.match(/QQ/i) == "qq") {
//在QQ空间打开
}
if (browser.versions.ios) {
//是否在IOS浏览器打开
}
if(browser.versions.android){
//是否在安卓浏览器打开
}