Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4596036
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:03:30+00:00 2026-05-21T23:03:30+00:00

I have this jQuery method which works fine if I am on a page

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:03:31+00:00Added an answer on May 21, 2026 at 11:03 pm

    Update: Re your updated code in the question:

    function replaceRightClickIcefacesMethod() {
        if (jQuery.browser.msie) {
            var elem = jQuery(".singlePaneOfGlassBlock"), oldName = elem
                    .attr("oncontextmenu"), fn = String;
    
            if (typeof oldName == "function") {
                oldName = elem[0].getAttributeNode("oncontextmenu").value,
                        fn = Function;
            }
    
            // ====> This is where the bug is, you're assuming you have
            //       a string at this point, but you don't necessarily,
            //       because if there is no match at all, `attr` will
            //       return `undefined` or `null` (I forget which).
            //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));
            })
        }
    }
    

    to fix:

    function replaceRightClickIcefacesMethod() {
        if (jQuery.browser.msie) {
            var elem = jQuery(".singlePaneOfGlassBlock"), oldName = elem
                    .attr("oncontextmenu"), fn = String;
    
            if (typeof oldName == "function") {
                oldName = elem[0].getAttributeNode("oncontextmenu").value,
                        fn = Function;
            }
    
            // ====> Add this `if`:
            if (oldName) {
                //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));
                });
            }
        }
    }
    

    Original answer:

    It seems to me you’re missing some braces (at least):

    function replaceRightClickIcefacesMethod() {
    if (jQuery.browser.msie) {
        var elem = jQuery(".singlePaneOfGlassBlock"), oldName = elem
                .attr("oncontextmenu"), fn = String;
        if (typeof oldName == "function") { // <=== Added { here
            oldName = elem[0].getAttributeNode("oncontextmenu").value,
                    fn = Function;
    
            oldName = oldName.replace('Ice.Menu.contextMenuPopup',
                    'contextMenuPopupUpdated');
            jQuery.each(elem, function() {
                this.setAttribute("oncontextmenu", fn(oldName));
            })
        } // <=== Added } here
    }
    }
    

    Here’s the above with the indentation corrected:

    function replaceRightClickIcefacesMethod() {
        if (jQuery.browser.msie) {
            var elem = jQuery(".singlePaneOfGlassBlock"), oldName = elem
                    .attr("oncontextmenu"), fn = String;
            if (typeof oldName == "function") { // <=== Added { here
                oldName = elem[0].getAttributeNode("oncontextmenu").value,
                        fn = Function;
    
                oldName = oldName.replace('Ice.Menu.contextMenuPopup',
                        'contextMenuPopupUpdated');
                jQuery.each(elem, function() {
                    this.setAttribute("oncontextmenu", fn(oldName));
                })
            } // <=== Added } here
        }
    }
    

    Here’s your original with the indentation corrected:

    function replaceRightClickIcefacesMethod() {
        if (jQuery.browser.msie) {
            var elem = jQuery(".singlePaneOfGlassBlock"), oldName = elem
                    .attr("oncontextmenu"), fn = String;
            if (typeof oldName == "function")
                oldName = elem[0].getAttributeNode("oncontextmenu").value,
                        fn = Function;
    
            // Note how these statements are NOT protected by the `if`
            // above, and so if `oldName` is `undefined` (as opposed to
            // being a String or Function.
            oldName = oldName.replace('Ice.Menu.contextMenuPopup',
                    'contextMenuPopupUpdated');
            jQuery.each(elem, function() {
                this.setAttribute("oncontextmenu", fn(oldName));
            })
        }
    }
    

    Or it may be that you’re missing an else clause and probably a further defensive if:

    function replaceRightClickIcefacesMethod() {
        if (jQuery.browser.msie) {
            var elem = jQuery(".singlePaneOfGlassBlock"), oldName = elem
                    .attr("oncontextmenu"), fn = String;
            if (typeof oldName == "function") {
                oldName = elem[0].getAttributeNode("oncontextmenu").value,
                        fn = Function;
            }
            else {    
                oldName = oldName.replace('Ice.Menu.contextMenuPopup',
                        'contextMenuPopupUpdated');
            }
            if (oldName) {
                jQuery.each(elem, function() {
                    this.setAttribute("oncontextmenu", fn(oldName));
                });
            }
        }
    }
    

    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…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this jQuery which works fine $(li[id^='shop_id']).click( function () { alert(I clicked on
On my main page I have this jquery code which does an ajax call
I have the following function which works fine when tied to an onClick() event..
I have this question: How could I call a codebehind method from jquery? I
I have this jQuery: $(document).ready(function() { $(#panel).hide(); $('.login').toggle( function() { $('#panel').animate({ height: 150, padding:20px
I have this jquery script which suppose to hide/show div element base on the
I have this code in jquery : $(#order_btn).click(function(){ var totalprice = $(.price_amount).text(); $(#totalprice).val(totalprice); });
I have this code in jQuery.. $(document).ready(function(){ var fieldCounter = 0; ... I have
I have a page that uses jQuery's $.post() method to get some html and
I have a WCF service, this is a method which I'd to call: [OperationContract]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.