Method to ignore JavaScript Errors globally instead of try... catch for each of cases

CK CK 3788 
Created at  
968 0 0

If you want to catch all errors in your script globally, you can use the window.onerror event handler. This catches errors from any part of your script and allows you to handle or ignore them:

window.onerror = function(message, source, lineno, colno, error) {
    // Handle the error (or simply ignore it)
    console.log("Global error caught: ", message);
    // Return true to prevent the default browser error handling
    return true;
};

// Example code that may throw an error
someFunctionThatMightThrowAnError();

// The script continues to run even if an error occurred
console.log("Script continues to run...");

By returning true from the window.onerror handler, you prevent the default browser error handling, effectively "ignoring" the error and allowing your script to continue running.

These methods ensure that errors do not interrupt your script's execution and allow you to handle errors gracefully.

Tags JavaScript Errors window.onerror Facebook X
Comments 0
  1. GraphQL Tutorial on CentOS 7: From Installation to Deployment
    555
  2. Capture all browser console logs including errors for debugging
    2,166
  3. Simple asynchronous/synchronous $.ajax() example using jQuery to make an HTTP GET request
    840
  4. JavaScript Code to Get Page Keywords
    344
  5. What is OpenGraph?
    364
  6. What is Twitter Cards?
    399
  7. Code to Extract the Site Thumbnail Image for Twitter or Facebook
    389
  8. JavaScript Code to Get the Page Title
    331
  9. JavaScript Code to Get Meta Description from HTML
    393
  10. Javascript code to extract favicon URL
    395
  11. RSS to Array in JavaScript
    2,138
  12. Javascript function to convert sitemap.xml to array
    595
  13. Understanding Elasticsearch from Installation to Trouble Shooting
    909
  14. IndexNow for better SEO
    316
  15. Enhancing SEO for Single Page Applications: Implementing Dynamic URL Updates
    1,037
  16. Method to catch the browser console log and errors for remote web debugging
    2,166
  17. Atlassian Jira Ticket API Response Example
    2,747
  18. Three different type of Word-Wrap options in HTML CSS
    1,181
  19. JavaScript code to load XML Document
    601
  20. Creating a Pinterest-Style Card Layout with Bootstrap and Masonry
    589
  21. Equal Height Blocks in Bootstrap with JavaScript
    708
  22. Copy text to clipboard in Javascript
    1,714
  23. To block copy & paste on your blog content
    899
  24. Shake your browser window by Javascript
    932
  25. Photoshop script programming in Javascript
    531
  26. To compare two HTML tables on web page in real time
    514
  27. To hide/show area of web page without re-loading a page
    1,018
  28. Displays the html tags without rendering on a webpage
    350
  29. Javascript based query parser for AJAX application
    564
  30. To get web browser capability
    522
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    27
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    24
  3. How do I create animated background for Google Meet?
    19,707
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    217
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    85
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    153
  7. Array of arguments passed to script in PHP
    85
  8. Invalid Auction Storms in Ad Tech
    93
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    197
  10. The Two Clocks of Ad Server Architecture
    63
  11. Frequency Cap Architecture Overview
    508
  12. Collecting Proof-of-Play from 100,000 Screens
    141
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    280
  14. What is RedisVL?
    105
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    224
  16. TPU & GPU Cluster Management Tools
    232
  17. The Concept of Scaffolding in AI Agents
    221
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    575
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    252
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    326
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    197
  22. Machine Learning: Training vs. Ranking
    213
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    217
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    189
  25. Designing a Data Curation System Around a Vector Database
    215
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    229
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    649
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    185
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    207
  30. Understanding Data Lineage
    252