strange npe in jQuery method
<p>I have this jQuery method which <strong>works fine if I am on a page where there are elements found</strong> which have class <code>.singlePaneOfGlassBlock</code>.</p>

<pre><code>function replaceRightClickIcefacesMethod() {
        //I only want this to run on IE for now, but the content 
        //is prepared to run on other browsers too
        if (jQuery.browser.msie) {
            var elem = jQuery(".singlePaneOfGlassBlock"), oldName = elem
                    .attr("oncontextmenu"), fn = String;

            //IE returns function instead of string so let's try to take the string
            //in the right way
            if (typeof oldName == "function") {
                oldName = elem[0].getAttributeNode("oncontextmenu").value,
                        fn = Function;
            }
            //do the replace
            oldName = oldName.replace('Ice.Menu.contextMenuPopup',
                    'contextMenuPopupUpdated');
            //change oncontextmenu with the new value
            jQuery.each(elem, function() {
                this.setAttribute("oncontextmenu", fn(oldName));
            })
        }
    }
</code></pre>

<p>But it failes with this error:</p>

<pre><code>`'undefined' is null or not an object`
</code></pre>

<p>on the line where it tries to do the replace if I am on other pages where those type of elements are missing...</p>

<p>I have add a checking to see if it's null or if it contains that string before doing the replace, but it still fails:</p>

<pre><code>if (jQuery(oldName).length || oldName.indexOf("Ice.Menu.contextMenuPopup") != -1) {
        oldName = oldName.replace('Ice.Menu.contextMenuPopup',
                'contextMenuPopupUpdated');
        jQuery.each(elem, function() {
            this.setAttribute("oncontextmenu", fn(oldName));
        })
    }
</code></pre>

<p>Can you give me a solution? </p>

<p>Thanks.</p>

<p><strong>UPDATE</strong>: when I'm asking a solution I'm just asking on how to correct this method to not run when that type of elements are not found?</p>
