#1160 new
Brian Marquis

Undefined Element Logging

Reported by Brian Marquis | October 22nd, 2010 @ 06:27 PM

When calling a prototype element function, the element extension $('non-existing-id') returns null, yet there is no way to discover the requested id at the point of failure. The following replacement for the function $(element) would provide console logging in the event that an id does not exist in the DOM.

function $(elementOrId) {
if (arguments.length > 1) {

for (var i = 0, elements = [], length = arguments.length; i < length; i++)
  elements.push($(arguments[i]));
return elements;

} if (Object.isString(elementOrId))

element = document.getElementById(elementOrId);

if ( !element && console ) {
  console.log("Warning: "+elementOrId+" is null.");
}

} else {

element = elementOrId;

}

return Element.extend(element); }

Comments and changes to this ticket

  • Victor

    Victor November 22nd, 2010 @ 07:11 PM

    Should be more effectively to wrap or replace original $() with this logging stuff in your custom helper script, and only if window.console and window.console.log are present (check for console will be evaluated only once, not every time you call $()).

    I think this is usually needed only for development, not for production environment.

  • Brian Marquis

    Brian Marquis November 23rd, 2010 @ 05:37 PM

    Contributing a wrapper function, perhaps this could go in the docs somewhere?

    function consolation() {
        if ( console && console.log && Prototype && !Prototype.$ ) {
            Prototype.$ = $;
            $ = function(elementOrId) {
                var element = Prototype.$(elementOrId);
                if ( element == null ) {
                    console.log(elementOrId+" is not an element or id");
                }
                return element;
            };
                    console.log("Consolation activated");
        }
    }
    

    To activate type consolation() in firebug or javascript:consolation() in your address bar. You can also add it to your document after prototype is loaded. Using Event.observe(document, "dom:loaded", consolation) will work but might prevent you from logging errors that occur before the dom is loaded.

    
    

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

The Prototype JavaScript library.

Shared Ticket Bins

People watching this ticket

Pages