Collection of commonly used js methods
- 2021-08-03 08:43:00
- OfStack
Deep copy of arrays and objects
var arr = [1,'2',{a:1,b:[1,2]}];
function deepCopy(p, c) {
var c = c || {};
for (var i in p) {
if (typeof p[i] === 'object' && p[i] !== null) { c[i] = (p[i].constructor === Array) ? [] : {}; deepCopy(p[i], c[i]);
} else {
c[i] = p[i];
}
}
return c;
}
var cArr = deepCopy(arr);
console.log(cArr);
Get Address Bar Parameters
function getUrlParam(){
var _arr = location.search.substr(1).split('&');
var _obj = {};
for (var i = 0; i < _arr.length; i++) {
_obj[_arr[i].split('=')[0]] = _arr[i].split('=')[1]
};
return _obj;
}
console.log(getUrlParam());
Modify WeChat title compatible with ios
function changeWxTitle(text){
var $body = $('body');
document.title = text;
var $iframe = $('<iframe src="/favicon.ico"></iframe>');
$iframe.on('load',function() {
setTimeout(function() {
$iframe.off('load').remove();
}, 0);
}).appendTo($body);
}
Mobile responsive style
/* Method is used in the head Tag addition 1 A style Label And there are .my-resize And .no-resize The style of, the elements that need to adapt to the screen are added .my-resize Class name will do, .no-resize Is to restore the adapted element
* window.onload = window.onresize = function(){
* pageResize({
* width : '320', // Default width 320px
* height : '504', // Default height 504px
* })
* }
*/
(function pageResize(opt) {
var ua = navigator.userAgent,
wp = ua.match(/Windows Phone ([\d.]+)/),
android = ua.match(/(Android);?[\s\/]+([\d.]+)?/),
// Initial ratio of width to height of equipment
dw = document.documentElement.clientWidth,
dh = document.documentElement.clientHeight,
ds = dw / dh,
// Initial ratio of page width to height
opt = opt || {},
pw = opt.width || 320,
ph = opt.height || 512,
ps = pw / ph;
// Core code: Page scaling
var sx = dw/pw,
sy = dh/ph;
var css = '.no-resize { -webkit-transform: scaleY('+sx/sy+');transform: scaleY('+sx/sy+'); }.my-resize { width:'+pw+'px !important;height:'+ph+'px !important;-webkit-transform: scale('+sx+','+sy+');transform: scale('+sx+','+sy+'); -webkit-transform-origin:left top;transform-origin:left top;}',
head = document.getElementsByTagName('head')[0],
style = document.createElement('style');
style.type = 'text/css';
if(style.styleSheet){
style.styleSheet.cssText = css;
}else{
style.appendChild(document.createTextNode(css));
}
head.appendChild(style);
})()