简体   繁体   中英

How can I hide an uncaught error from my browser?

I have some simple code that does the job but gives an uncaught error.

 ck.setMode('source');
 ck.setMode( 'wysiwyg'); 

This is giving me the message:

Uncaught TypeError: Cannot read property 'on' of undefined 

I know it's probably a bad practice but the error does not cause any problems and I would like to avoid it showing in the browser. Is there some way that I could enclose this code so it does not give a browser console alert?

Here's the code that encloses the above:

ngModel.$render = function () {
    if (typeof ngModel.$modelValue != 'undefined') {
        if (ngModel.$modelValue != null) {
            ck.setData(ngModel.$modelValue);
             timer = setTimeout(function () {
                ck.setData(ngModel.$modelValue);
             }, 1000);
             timer = setTimeout(function () {
                 ck.setMode('source');
                 ck.setMode('wysiwyg');
             }, 1000);
        }   
    }
};

You may use code like this:

window.onerror = function(message, url, lineNumber) {  
    // maybe some handling?  
    return true; // prevents browser error messages  
};

It prevents all error messages, so use it with care.

You can put your code block inside a try catch. So your code would become like this.

try {
   ck.setMode('source');
   ck.setMode( 'wysiwyg'); 
}
catch (error) {
   // handle your error
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM