Mimic Window. onerror in Opera using javascript

后端 未结 4 1696
眼角桃花
眼角桃花 2021-01-17 21:40

I am currently working on a web application, I have a JS logging mechanism that Handles Javascript error that are not caught by the js code inside the page. I am using windo

4条回答
  •  北恋
    北恋 (楼主)
    2021-01-17 22:39

    you can replace Error.prototype.toString in Opera!

    window.onerror = function (msg) {
      // send msg to http://errors.net/log.php, for example
      (new Image()).src = 'http://errors.net/log.php?msg=' + encodeURIComponent(msg);
    };
    if (({}).toString.call(window.opera) === '[object Opera]') {
     (function () {
       var x = Error.prototype.toString;
       Error.prototype.toString = function () {
         var msg = '';
         try {
           msg = x.apply(this, arguments);
           if (typeof (window.onerror) === "function") {
             window.onerror(msg, typeof (this) === 'object' ? this.stack : '', '');
           }
         } catch (e) {}
         return msg;
       };
     }());
    }
    

    seems, it doesn't work for Opera 11.50... only for early versions ...

提交回复
热议问题