面对复杂的前端环境,进行前端异常日志记录很有必要,基于 window.onerror 实现:
// 基于window.onerror 收集前端错误信息
window.onerror = function (message, url, line) {
if (!url) return;
var msg = {
ua: window.navigator.userAgent,
message: message,
url: url,
line: line,
page: window.location.href
};
new Image().src = '/Home/UiErrorLog?error=' encodeURIComponent(JSON.stringify(msg)) '&t=' Math.random();
};
更多专业前端知识,请上 【猿2048】www.mk2048.com