if (!!window.ActiveXObject || "ActiveXObject" in window){
alert('IE浏览器')
}else{
alert('不是IE浏览器')
}
+new Date()
new Date().getTime()
new Date().valueOf()
window.print()
!!navigator.geolocation;
//在HTML 5中,navigator.geolocation可以获取设备的当前位置,通过双“!”就可以判断是否支持此API,即是否支持HTML 5
var osType = "",
windows = (navigator.userAgent.indexOf("Windows",0) != -1)?1:0,
mac = (navigator.userAgent.toLowerCase().indexOf("mac",0) != -1)?1:0,
linux = (navigator.userAgent.indexOf("Linux",0) != -1)?1:0,
unix = (navigator.userAgent.indexOf("X11",0) != -1)?1:0;
%%
if (windows) osType = "Windows";
else if (mac) osType = "Mac";
else if (linux) osType = "Lunix";
else if (unix) osType = "Unix";
console.log(osType);
//navigator.userAgent表示用户代理。
var mobileReg = /iphone|ipod|android.*mobile|windows.*phone|blackberry.*mobile/i;
if((mobileReg.test(window.navigator.userAgent.toLowerCase()))){
alert("移动设备!");
}else{
alert("非移动设备!");
}
7.数组去重
const uniqueArr = (arr) => [...new Set(arr)];
console.log(uniqueArr(["前端","js","html","js","css","html"]));
// ['前端', 'js', 'html', 'css']
const getParameters = URL => JSON.parse(`{"${decodeURI(URL.split("?")[1]).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"')}"}`
)
getParameters("https://www.google.com.hk/search?q=js+md&newwindow=1");
// {q: 'js+md', newwindow: '1'}
9.检查对象是否为空
const isEmpty = obj => Reflect.ownKeys(obj).length === 0 && obj.constructor === Object;
isEmpty({}) // true
isEmpty({a:"not empty"}) //false
const reverse = str => str.split('').reverse().join('');
reverse('this is reverse');
// esrever si siht
const randomHexColor = () => `#${Math.floor(Math.random() * 0xffffff).toString(16).padEnd(6, "0")}`
console.log(randomHexColor());
// #a2ce5b
12.检查当前选项卡是否在后台
const isTabActive = () => !document.hidden;
isTabActive()
// true|false
const elementIsInFocus = (el) => (el === document.activeElement);
elementIsInFocus(anyElement)
// 元素处于焦点返回true,反之返回false
14.检查设备类型
const judgeDeviceType =
() => /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|OperaMini/i.test(navigator.userAgent) ? 'Mobile' : 'PC';
judgeDeviceType() // PC | Mobile
15.文字复制到剪贴板
const copyText = async (text) => await navigator.clipboard.writeText(text)
copyText('单行代码 前端世界')
const getSelectedText = () => window.getSelection().toString();
getSelectedText();
// 返回选中的内容
const isWeekday = (date) => date.getDay() % 6 !== 0;
isWeekday(new Date(2022, 03, 11))
// true
- 将华氏温度转换为摄氏温度
const fahrenheitToCelsius = (fahrenheit) => (fahrenheit - 32) * 5/9;
fahrenheitToCelsius(50);
// 10
- 将摄氏温度转华氏温度
const celsiusToFahrenheit = (celsius) => celsius * 9/5 + 32;
celsiusToFahrenheit(100)
// 212
const dayDiff = (date1, date2) => Math.ceil(Math.abs(date1.getTime() - date2.getTime()) / 86400000);
dayDiff(new Date("2021-10-21"), new Date("2022-02-12"))
// Result: 114
const average = (arr) => arr.reduce((a, b) => a + b) / arr.length;
average([1,9,18,36]) //16