Override window.alert

By  on  

For years the only bit of feedback web developers could get was via alert("{str}") calls. These days we have the web console but, in rare cases, we don't have a console and alert calls are our only window into a value at a given time.

One problem: if an alert sneaks into production code, your site looks like it's been hacked. Your site looks like it's malware! To prevent any of those issues, you can add this snippet to your production build:

window.alert = console.log

This tiny line of JavaScript could save your site from catastrophe. There are many cases for overriding native functionality and this is a great example!

Recent Features

  • By
    5 Awesome New Mozilla Technologies You’ve Never Heard Of

    My trip to Mozilla Summit 2013 was incredible.  I've spent so much time focusing on my project that I had lost sight of all of the great work Mozillians were putting out.  MozSummit provided the perfect reminder of how brilliant my colleagues are and how much...

  • By
    Create a CSS Flipping Animation

    CSS animations are a lot of fun; the beauty of them is that through many simple properties, you can create anything from an elegant fade in to a WTF-Pixar-would-be-proud effect. One CSS effect somewhere in between is the CSS flip effect, whereby there's...

Incredible Demos

  • By
    Google-Style Element Fading Using MooTools or jQuery

    Google recently introduced an interesting effect to their homepage: the top left and top right navigation items don't display until you move your mouse or leave the search term box. Why? I can only speculate that they want their homepage as...

  • By
    MooTools Gone Wild: Element Flashing

    If you're like me and lay awake in bed at night, you've flipped on the TV and seen the commercials: misguided, attention-starved college girls fueled by alcohol ruining their futures by flashing lame camera-men on Spring Break. Why do they do it? Attention...

Discussion

  1. jonathan santos

    it’s an interesting idea, and I’m not saying the world is perfect, but if instead of using this to avoid pushing debug code onto production, how would I go about testing for stuff like alerts and other weird edge cases

  2. Mr P

    In older IE browsers the browser will crash if console.log is called when the debug window is closed. Will this work then?

  3. dev

    override window.alert in IE for logggint using $.ajax to call WebService or REST API ?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!