Amazon.com Widgets November 2010

WilliaBlog.Net

I dream in code

About the author

Robert Williams is an internet application developer for the Salem Web Network.
E-mail me Send mail
Code Project Associate Logo
Go Daddy Deal of the Week: 30% off your order at GoDaddy.com! Offer expires 11/6/12

Recent comments

Archive

Authors

Tags

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.


JQuery Code only working correctly when Firebug is open

If you ever come across a situation where a page on your site is not working as it should, but when you open Firebug to see what is happening it all works fine, chances are that somewhere in your Javascript/JQuery code there is a console.log() statement. If Firebug is not open, console will be undefined and the rest of the scripts may not run. In my case, the problem was reported that clicking the save button on an html form resulted in the Json results prompting “Save As” dialog like the one below, instead of being processed:

 

Json results prompting “Save As” dialog in browser instead of being processed

Naturally, I spent most of the afternoon looking into why JSON results might prompt you with a Save As Dialog in an MVC application, and none of the reported fixes was working. I opened Firebug to look at some html and noticed that suddenly the page looked different - there was a lot more formatting, and it just looked like this was how the page was supposed to look. Not only that, but with Firebug open, the Save button correctly used Ajax to submit the form and the Save as Dialog didn't popup. So I closed Firebug and refreshed the page. Broken Again. Luckily for me, a collegue had recently had the same problem last week, and pointed out that a console.log statement had been responsible. There were no console.log statements on the page in question, but a global Find operation located one on the master page. Commenting it out made everything work as it was supposed to.

The moral of the story is that if you use console.log statements while debugging, make sure you remove them all when you are done, or better yet, create a global Javascript function that you can call from anywhere which first ensures that console is not undefined. Something like this should work:

    function debug(text) {
        if ((typeof(Debug) !== 'undefined') && Debug.writeln) {
            Debug.writeln(text);
        }
        if (window.console && window.console.log) {
            window.console.log(text);
        }
        if (window.opera) {
            window.opera.postError(text);
        }
        if (window.debugService) {
            window.debugService.trace(text);
        }
    }


Categories: JavaScript | JQuery
Posted by Williarob on Monday, November 01, 2010 6:20 AM
Permalink | Comments (0) | Post RSSRSS comment feed