Check whether the device is a touchscreen device
Check whether touch is supported
var isTouchDevice = 'ontouchstart' in document.documentElement;
console.log(isTouchDevice);
Copy the code
Computer browsers support mouse events, but mobile phones do not. Therefore, it can be discriminated by detecting whether touch screen is supported.
Check whether the device is a mobile phone
var isMobile = /Android|webOS|iPhone|iPad|BlackBerry/i.test(navigator.userAgent)
if (isMobile) {
// The current page is opened on the mobile phone
} else {
// The current page is opened on the PC
}
Copy the code
Js controls media query
var result = window.matchMedia('(max-width: 768px)');
if (result.matches) {
//console.log(' page width less than or equal to 768px');
// Write mobile js effects
} else {
//console.log(' page width > 768px');
// Write a js effect with a page width greater than 768px
}
Copy the code
Template string
In ES6, JS has template strings that can replace quoted concatenated strings.
Ordinary string and variable concatenation
let name = 'Alice';
let id = 1024;
console.log(name + "\'s id is " + id);
Copy the code
Template string: concatenated with ${variable}
let name = 'Alice';
let id = 1024;
console.log(`${name}'s id is ${id}`);
Copy the code
Concatenate data received by the HTTP module: buffer.concat ()
When we receive uploaded data using the NodeJS HTTP module, we may receive UTF-8 encoding, such as “2F 2a 2a 0A 20 2A 20 53 75”.
At this point we can use buffer. concat to respell the data back into characters.
request.on('data', (chunk) =>{
file_array.push(chunk)
});
request.on('end',() =>{
const file_string = Buffer.concat(file_array).toString();
console.log(file_string);
});
Copy the code