This project is read-only.

ContextMenuJS.js - Added right click fix for Firefox browsers

Topics: Developer Forum, User Forum
Feb 24, 2007 at 11:10 PM
I needed the context menu to work in Firefox as well as IE so here is the explanation to get it working.

// in ContextMenu.cs, change private const string AttachContextMenu = "return __showContextMenu({0});";
// to private const string AttachContextMenu = "return __showContextMenu({0}, event);";

// then add the event argument to the __showContextMenu javascript function definition

// the rest takes care of Firefox


function __showContextMenu(menu, event)
{
var browserName = whichBrs();

var menuOffset = 2;
if (browserName == "Internet Explorer") {
menu.style.left = window.event.x - menuOffset;
menu.style.top = window.event.y - menuOffset;
menu.style.display = "";
window.event.cancelBubble = true;

}

if (browserName == "Firefox") {
var btn = whichButton(event);
if (btn == 2)
{
menu.style.left = event.clientX - menuOffset;
menu.style.top = event.clientY - menuOffset;
menu.style.display = "";
event.cancelBubble = true;
}
}
return false;
}

function whichButton(e)
{
// Handle different event models
var e = e || window.event;
var btnCode;

if ('object' == typeof e){
btnCode = e.button;
return btnCode;
}
}

function __trapESC(menu)
{
var brwsr = whichBrs();
if (brwsr == "Internet Explorer")
{
var key = window.event.keyCode;
if (key == 27)
{
menu.style.display = 'none';
}
}
}

function whichBrs() {
var agt=navigator.userAgent.toLowerCase();
if (agt.indexOf("opera") != -1) return 'Opera';
if (agt.indexOf("staroffice") != -1) return 'Star Office';
if (agt.indexOf("webtv") != -1) return 'WebTV';
if (agt.indexOf("beonex") != -1) return 'Beonex';
if (agt.indexOf("chimera") != -1) return 'Chimera';
if (agt.indexOf("netpositive") != -1) return 'NetPositive';
if (agt.indexOf("phoenix") != -1) return 'Phoenix';
if (agt.indexOf("firefox") != -1) return 'Firefox';
if (agt.indexOf("safari") != -1) return 'Safari';
if (agt.indexOf("skipstone") != -1) return 'SkipStone';
if (agt.indexOf("msie") != -1) return 'Internet Explorer';
if (agt.indexOf("netscape") != -1) return 'Netscape';
if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
if (agt.indexOf('\/') != -1) {
if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
return navigator.userAgent.substr(0,agt.indexOf('\/'));}
else return 'Netscape';}
else if (agt.indexOf(' ') != -1)
return navigator.userAgent.substr(0,agt.indexOf(' '));
else return navigator.userAgent;
}
Dec 23, 2008 at 6:06 PM
Hello, 

Thank you for working this issue out.  After implmenting the code you listed, I am still receiving an error on a line in the method "PrepareControlHierarchy"....(see below).  Based on your proposed fix, there might need to be something added here.  Also, my version of xgrid (which I downloaded from codeplex) also passes the RowID to the "__showContextMenu" (didnt see that in your example)....  I would assume something is missing.  Also, it is not clear to me how "event" ever gets assigned.  Any help you can give would be appreciated.


                // if ContextMenuID is not null, means ContextMenu should be enabled.
                if (!string.IsNullOrEmpty(ContextMenuID))
                {
                    for (int i = 0; i < (base.Rows.Count); i++)
                    {
                        //argsData = "rc" + base.Rows[i].RowIndex.ToString();
                        this.Rows[i].Attributes.Add("oncontextmenu", "return __showContextMenu(" + ContextMenuID + "_Root, " + base.Rows[i].RowIndex + ");");
                    }
                }



Sep 29, 2009 at 7:00 AM

Hi

I tried on this issue this not fixing means still not working in Firefox please suggest me any solution thank you:)