JavaScript 中的一些常用方法
//克隆对象
function CloneObj (obj) { var newObj = {} for(var prop in obj) { newObj[prop] = obj[prop] } return newObj; }
//去重
function Trim(arr) { var res = []; var json = {}; for(var i = 0; i < arr.length; i++) { if(!json[arr[i]]) { res.push(arr[i]); json[arr[i]] = 1; } } return res; }
//获取当前点的目标对象
function(point) { var res = document.elementFromPoint(point.x, point.y) return res; }
//URL参数 function Request(m) { var sValue = location.search.match(new RegExp("[\?\&]" + m + "=([^\&]*)(\&?)", "i")); return sValue ? sValue[1] : sValue; } function UrlUpdateParams (url, name, value) { var r = url; if(r != null && r != 'undefined' && r != "") { value = encodeURIComponent(value); var reg = new RegExp("(^|)" + name + "=([^&]*)(|$)"); var tmp = name + "=" + value; if(url.match(reg) != null) { r = url.replace(eval(reg), tmp); } else { if(url.match("[\?]")) { r = url + "&" + tmp; } else { r = url + "?" + tmp; } } } return r; }
//获取图片路径 function GetPicFullPath(obj, selector) { if(obj) { //Internet Explorer if(window.navigator.userAgent.indexOf("MSIE") >= 1) { obj.select(); return document.selection.createRange().text; } //Firefox if(window.navigator.userAgent.indexOf("Firefox") >= 1) { if(obj.files) { return obj.files.item(0).getAsDataURL(); } return obj.value; } //兼容chrome、火狐等,HTML5获取路径 if(typeof FileReader != "undefined") { var reader = new FileReader(); reader.onload = function(e) { selector[0].style.backgroundImage = "url(" + e.target.result + ")"; } reader.readAsDataURL(obj.files[0]); } else if(browserVersion.indexOf("SAFARI") > -1) { alert("暂时不支持Safari浏览器!"); } } }