63

How can we debug JavaScript with IE 8 ?

The JavaScript debbuging with Visual Studio doesn't work after an update to IE 8.

3
  • 1
    Thanks for the help, although ideally that would have been a response to one of the question you mentioned in the post. The 'Ask Question' in Stackoverflow is just for asking questions! Aug 20, 2009 at 13:38
  • 1
    It's great that you want to help. But the way you do this is to Ask a question, and then post this as a response to the question you asked. Aug 20, 2009 at 13:41
  • Is it better like this ? Aug 20, 2009 at 13:42

4 Answers 4

81

I discovered today that we can now debug Javascript With the developer tool bar plugins integreted in IE 8.

  • Click ▼ Tools on the toolbar, to the right of the tabs.
  • Select Developer Tools. The Developer Tools dialogue should open.
  • Click the Script tab in the dialogue.
  • Click the Start Debugging button.

You can use watch, breakpoint, see the call stack etc, similarly to debuggers in professional browsers.

You can also use the statement debugger; in your JavaScript code the set a breakpoint.

5
  • 2
    You can set breakpoints without the debugger; statement. IE8 is pretty nice for JS debugging, ESPECIALLY since you can render and parse like IE7, with IE8 tools. Aug 20, 2009 at 13:59
  • 2
    @Reinis I. Did you put a ; after your debugger; ?? Nov 11, 2011 at 15:27
  • I was almost about to curse IE and then I read your answer. Thanks! Mar 29, 2013 at 18:06
  • 6
    @VaibhavDesai I'm still cursing IE 8. The debugging capabilities are so poor.
    – async
    Jun 30, 2014 at 10:23
  • Why would anybody curse IE? Apart from the fact that it still doesn't support the <sarcasm> tag? Nov 20, 2015 at 23:41
8

You can get more information about IE8 Developer Toolbar debugging at Debugging JScript or Debugging Script with the Developer Tools.

6

This won't help you step through code or break on errors, but it's a useful way to get the same debug console for your project on all browsers.

myLog = function() {
    if (!myLog._div) { myLog.createDiv(); }

    var logEntry = document.createElement('span');
    for (var i=0; i < arguments.length; i++) {
        logEntry.innerHTML += myLog.toJson(arguments[i]) + '<br />';
    }
    logEntry.innerHTML += '<br />';

    myLog._div.appendChild(logEntry);
}
myLog.createDiv = function() {
    myLog._div = document.body.appendChild(document.createElement('div'));
    var props = {
        position:'absolute', top:'10px', right:'10px', background:'#333', border:'5px solid #333', 
        color: 'white', width: '400px', height: '300px', overflow: 'auto', fontFamily: 'courier new',
        fontSize: '11px', whiteSpace: 'nowrap'
    }
    for (var key in props) { myLog._div.style[key] = props[key]; }
}
myLog.toJSON = function(obj) {
    if (typeof window.uneval == 'function') { return uneval(obj); }
    if (typeof obj == 'object') {
        if (!obj) { return 'null'; }
        var list = [];
        if (obj instanceof Array) {
            for (var i=0;i < obj.length;i++) { list.push(this.toJson(obj[i])); }
            return '[' + list.join(',') + ']';
        } else {
            for (var prop in obj) { list.push('"' + prop + '":' + this.toJson(obj[prop])); }
            return '{' + list.join(',') + '}';
        }
    } else if (typeof obj == 'string') {
        return '"' + obj.replace(/(["'])/g, '\\$1') + '"';
    } else {
        return new String(obj);
    }
}

myLog('log statement');
myLog('logging an object', { name: 'Marcus', likes: 'js' });

This is put together pretty hastily and is a bit sloppy, but it's useful nonetheless and can be improved easily!

3

I was hoping to add this as a comment to Marcus Westin's reply, but I can't find a link - maybe I need more reputation?


Anyway, thanks, I found this code snippet useful for quick debugging in IE. I have made some quick tweaks to fix a problem that stopped it working for me, also to scroll down automatically and use fixed positioning so it will appear in the viewport. Here's my version in case anyone finds it useful:

myLog = function() {

    var _div = null;

    this.toJson = function(obj) {

        if (typeof window.uneval == 'function') { return uneval(obj); }
        if (typeof obj == 'object') {
            if (!obj) { return 'null'; }
            var list = [];
            if (obj instanceof Array) {
                    for (var i=0;i < obj.length;i++) { list.push(this.toJson(obj[i])); }
                    return '[' + list.join(',') + ']';
            } else {
                    for (var prop in obj) { list.push('"' + prop + '":' + this.toJson(obj[prop])); }
                    return '{' + list.join(',') + '}';
            }
        } else if (typeof obj == 'string') {
            return '"' + obj.replace(/(["'])/g, '\\$1') + '"';
        } else {
            return new String(obj);
        }

    };

    this.createDiv = function() {

        myLog._div = document.body.appendChild(document.createElement('div'));

        var props = {
            position:'fixed', top:'10px', right:'10px', background:'#333', border:'5px solid #333', 
            color: 'white', width: '400px', height: '300px', overflow: 'auto', fontFamily: 'courier new',
            fontSize: '11px', whiteSpace: 'nowrap'
        }

        for (var key in props) { myLog._div.style[key] = props[key]; }

    };


    if (!myLog._div) { this.createDiv(); }

    var logEntry = document.createElement('span');

    for (var i=0; i < arguments.length; i++) {
        logEntry.innerHTML += this.toJson(arguments[i]) + '<br />';
    }

    logEntry.innerHTML += '<br />';

    myLog._div.appendChild(logEntry);

    // Scroll automatically to the bottom
    myLog._div.scrollTop = myLog._div.scrollHeight;

}
2
  • I don't get it. How can I use this in IE8 to debug javascript please? Run javascript to debug javascript?
    – Fandango68
    Jul 7, 2016 at 4:43
  • 1
    @Fernando68 It's JavaScript to add a debug console that you can see on the page, which will display things you log with myLog('i want to see this', fooObject);. I think you shouldn't really need it with IE8 because as long as you have the developer tools open, you can use console.log, step-through debug etc. But if you find yourself having to debug even older browsers, perhaps it may help. Thankfully I haven't had to use this for a long time myself! Jul 8, 2016 at 6:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.