Bookmarklet to enable debug on a webpage

Often when working on a webpage in either PHP or Javascript I'll want to enable some form of debug mode. Usually this involves adding (or removing) debug=1 to the end of the URL. I created a simple bookmarklet to automate this process. I call it debugify. Drag the link to your quick launch bar to add it to your browser.

Here is the raw code which I then run through Bookmarklet Crunchinator

var url = location.href;
var new_url = '';
var match = url.match(/debug=(\d)/);
var url_char = '?';

if (url.match(/\?/)) {
    url_char = '&';
}

var current_debug = 0;
if (match) {
    current_debug = match[1];
    var new_debug = 1;

    if (current_debug > 0) {
        new_debug = 0;
    }

    new_url = url.replace(/debug=(\d)/, 'debug=' + new_debug);
} else if (url.match(/#/)) {
    new_url = url.replace(/#/, url_char + 'debug=1#');
} else {
    new_url = url + url_char + 'debug=1';
}

location.href = new_url;
Leave A Reply
All content licensed under the Creative Commons License