Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

« Newer Snippets
Older Snippets »
2 total  XML / RSS feed 

Javascript Runtime breakpoints

var TrimPath;
if (TrimPath == null)
    TrimPath = new Object();

/**
 * TrimPath.breakpoint usage: 
 *
 * In the middle of your code somewhere, add a line of code like...
 *
 *   breakpoint(function(expr) { return eval(expr); });
 *
 * You can also pass a message, like...
 *
 *   breakpoint(function(expr) { return eval(expr); }, "breakpoint #2 in datetime validation");
 *
 * You can also pass in an initial expression as the 3rd parameter, like...
 *
 *   breakpoint(function(expr) { return eval(expr); }, "breakpoint #2 in datetime validation", "dateStr");
 *
 * Then, you can enter expressions in the prompt dialog to inspect variables and objects
 * in the breakpoint's scope.  Click Cancel in the prompt dialog to continue processing.
 */
var breakpoint = TrimPath.breakpoint = function(evalFunc, msg, initialExprStr) { 
    // TrimPath.breakpoint currently works only in DOM/browser environment.
    if (msg == null)
        msg = "";
    var result = initialExprStr || "1+2";
    while (true) {
        var expr = prompt("BREAKPOINT: " + msg + "\nEnter an expression to evaluate, or Cancel to continue.", result); 
        if (expr == null || expr == "")
            return;
        try {
            result = evalFunc(expr);
        } catch (e) {
            result = e;
        }
    }
}


Then, at runtime, when the breakpoint line is reached, execution will stop and a prompt dialog will open where you can enter JavaScript expressions in order to...

* Inspect variables and objects in the breakpoint's scope. For example, "document.getElementById('foo').style.width"
* Set and modify variable values, too. For example, "currCounter = 0"
* Additionally, you can also call alert() from the breakpoint prompt dialog, such as when you want to inspect something with a large output value. For example, "alert(myHiddenTextarea.value)"

Using the console in FireFox

console.log(format, obj1, ...)


The console object provides methods for displaying log messages in the console, it's more flexible than calling alert, the console.log() method is similar to C's printf, it takes a formatting string and optional additional parameters, and outputs the formatted string to the console:

var i = 1;
console.log('i value is %d', i);
// prints:
// i value is 3


console.trace()


This method prints a stack trace where it's called, it doesn't have any parameters.

inspect(obj)


This function takes one parameter, it switches to the inspection tab and inspects the passed object.
« Newer Snippets
Older Snippets »
2 total  XML / RSS feed