MaiOM
MaiOM

Reputation: 936

Javascript events in Firefox and IE9

I attach in my ASP.NET app to a cell oncontextmenu a function string.Format("OnCellContextMenu({0}, '{1}', true, true)", e.VisibleIndex, e.DataColumn.FieldName).

In my JS i define the following function.

function OnCellContextMenu(visibleIndex, fieldName, hasNote, hasValue) {
    currentVisibleIndex = visibleIndex;
    currentFieldName = fieldName;

    if (fieldName == "Name" || fieldName == "TOTAL") {
        EnableMenuItem('AddNote', false);
        EnableMenuItem('EditNote', false);
        EnableMenuItem('RemoveNote', false);
    }
    else {
        EnableMenuItem('AddNote', !hasNote && hasValue);
        EnableMenuItem('EditNote', hasNote);
        EnableMenuItem('RemoveNote', hasNote);
    }

    window.event.returnValue = false;

    gvPrevisions.SetFocusedRowIndex(visibleIndex);
    GridMenu.ShowAtPos(ASPxClientUtils.GetEventX(event), ASPxClientUtils.GetEventY(event));
}

Now, on IE works properlly, but on Firefox window.event.returnValue = false; it is not executed. I googled around to see how the Firefox treats this return value and I got that I should call e.preventDefault(); insdead of window.event. The problem is that in my function e is undefined.

Can you please help me finding a solution that works both on FF and IE?

Thanks

Upvotes: 2

Views: 643

Answers (2)

MaiOM
MaiOM

Reputation: 936

I found my way out.

I declared a function in this way:

function OnCellContextMenu(e, visibleIndex, fieldName, hasNote, hasValue)

and the association in this way:

e.Cell.Attributes.Add("oncontextmenu", string.Format("OnCellContextMenu(event, {0}, '{1}', true, true)", e.VisibleIndex, e.DataColumn.FieldName));

then in the function I got var currentEvent = (window.event) ? window.event : e; and used currentEvent across other function calls like ASPxClientUtils.PreventEventAndBubble(currentEvent);

Cheers

Upvotes: 0

Dogoku
Dogoku

Reputation: 4675

You can refactor your code to use jQuery, which works across all browsers E.g:

$('body').bind('contextmenu', function(e) {
    //Stop browser from opening context menu
    e.preventDefault();
    //Do more stuff
});

Upvotes: 1

Related Questions