I have this jQuery method which works fine if I am on a page where there are elements found which have class .singlePaneOfGlassBlock.
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));
})
}
}
But it failes with this error:
`'undefined' is null or not an object`
on the line where it tries to do the replace if I am on other pages where those type of elements are missing…
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:
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));
})
}
Can you give me a solution?
Thanks.
UPDATE: 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?
Update: Re your updated code in the question:
to fix:
Original answer:
It seems to me you’re missing some braces (at least):
Here’s the above with the indentation corrected:
Here’s your original with the indentation corrected:
Or it may be that you’re missing an
elseclause and probably a further defensiveif:But it’s really hard to say, I can’t quite follow the logic. The function path part of it doesn’t seem to change anything, but the string path part does…