Skip to content

Instantly share code, notes, and snippets.

View gebin's full-sized avatar

gebin

  • 车300
  • 南京
View GitHub Profile

捕捉浏览器中的JS运行时错误,主要通过监听window.onerror来实现。但是对于不同的脚本执行方式以及不同的浏览器,能捕获到的信息会有区别。

window.onerror 讲接收3个参数:

  • msg:错误描述,比如:a is not defined
  • url:出错脚本所在的url
  • lineNumber:出错脚本的行数

本文将对不同浏览器和不同的脚本执行方式进行测试,并总结这些区别。

@gebin
gebin / input-focus-keybord-fixed.js
Created April 6, 2017 05:56
Android下,input被键盘遮挡的问题
// .container 设置了 overflow 属性, 导致 Android 手机下输入框获取焦点时, 输入法挡住输入框的 bug
// 相关 issue: https://github.com/weui/weui/issues/15
// 解决方法:
// 0. .container 去掉 overflow 属性, 但此 demo 下会引发别的问题
// 1. 参考 http://stackoverflow.com/questions/23757345/android-does-not-correctly-scroll-on-input-focus-if-not-body-element
// Android 手机下, input 或 textarea 元素聚焦时, 主动滚一把
if (/Android/gi.test(navigator.userAgent)) {
window.addEventListener('resize', function () {
if (document.activeElement.tagName == 'INPUT' || document.activeElement.tagName == 'TEXTAREA') {
@gebin
gebin / valid-mobile-format.js
Created April 6, 2017 05:57
验证手机号
/**
* 验证手机号
* @param {String} mobile 手机号码
* @return {Boolean} 校验结果
*/
function validMobileFormat(mobile) {
var numberRegStr = /^\d{11}$/;
var regNum = new RegExp(numberRegStr);
if (regNum.test(mobile)) {
var telRegStr = /^(13|14|18|15|17)\d{9}$/;
@gebin
gebin / parse-query-string.js
Created April 6, 2017 05:58
解析url字符串到json对象
/**
* URL to JSON
* @param {String} url 网址
* @return {Object} json对象
*/
function parseQueryString(url) {
var reg_url = /^[^\?]+\?([\w\W]+)$/,
reg_para = /([^&=]+)=([\w\W]*?)(&|$|#)/g,
arr_url = reg_url.exec(url),
ret = {};
@gebin
gebin / memorySizeOfObject.js
Created January 3, 2018 12:33
calculate memory size of javascript object, it is not a accurate value!
function memorySizeOf(obj) {
var bytes = 0;
function sizeOf(obj) {
if(obj !== null && obj !== undefined) {
switch(typeof obj) {
case 'number':
bytes += 8;
break;
case 'string':