locked
HtmlPage.Document.AttachEvent does not fire for IE11 RRS feed

  • Question

  • The existing functionality to AttachEvent from silverlight xaml.cs page stopped working after my client moved to IE11. looks like this event does not fire. I am sharing with you a sample code which mimics this functionality. When a user right clicks on the silverlight page, "FireOnContextMenu" is invoked. This in-turn calls the "onContextMenu" method available in the xaml.cs page. In IE11 mode, apart from firing the "oncontextmenu", i am also firing another event to change the span color for testing purpose.

    Please note that the "oncontextmenu" gets fired when the browser compatiblity mode has been set.


    The xaml page has the following code:

    public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
                this.Loaded += MainPage_Loaded;
            }

            void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
                HtmlPage.Document.AttachEvent("oncontextmenu", OnContextMenu);
                
                
                System.Windows.Application.Current.RootVisual.MouseRightButtonDown +=
                   new System.Windows.Input.MouseButtonEventHandler(_OnMouseRightButtonDown);

                System.Windows.Application.Current.RootVisual.MouseRightButtonUp +=
                    new System.Windows.Input.MouseButtonEventHandler(_OnMouseRightButtonUp);
            }

            private void OnContextMenu(object sender, HtmlEventArgs e)
            {
                MessageBox.Show("hello");
            }

            private void _OnMouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
            {
                if (Application.Current.Host.Settings.Windowless == false)
                {
                    if ((Keyboard.Modifiers & ModifierKeys.Control) == 0)
                    {
                        e.Handled = true;
                    }
                }
            }

            private void _OnMouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
            {
                if (Application.Current.Host.Settings.Windowless == false)
                {
                    e.Handled = true;
                    if ((Keyboard.Modifiers & ModifierKeys.Shift) == 0)
                    {
                        HtmlPage.Window.Invoke("FireOnContextMenu", null);
                    }
                }
            }
        }

    The javascript within the aspx page is as follows:

    function FireOnContextMenu() {
                if (document.createEventObject) // IE before version 9
                {
                    alert('createEventObject');
                    var mousedownEvent = document.createEventObject();
                    mousedownEvent.button = 2;  // right button is down
                    document.body.fireEvent("oncontextmenu", mousedownEvent);
                }
                else if (document.createEvent) // all browsers except IE before version 9
                {
                    CallDispatch('contextmenu', document.body);
                    CallDispatch('click', document.getElementById('spn1'));
                }
            }

            function CallDispatch(evtName, element)
            {
                var ev = document.createEvent("Event");
                ev.initEvent(evtName, true, false);
                try {
                    element.dispatchEvent(ev);
                }
                catch (err) {
                    alert(err);
                }
            }
            function ChangeColor(elem)
            {
                if (elem)
                {
                    if (elem.style) {
                        if (elem.style.backgroundColor == 'green') {
                            elem.style.backgroundColor = 'red';
                        }
                        else {
                            elem.style.backgroundColor = 'green';
                        }
                    }
                }
            }

    As per msdn, AttachEvent is no longer supported for IE11.

    Is this the reason for it not to work? Is there any other alternative.

    Please advice

    Thursday, September 29, 2016 10:59 AM

Answers

  • Thanks for the reply, Weiwei.

    I had already noted that AttachEvent is no longer supported in IE11. I was looking at a way to call the below code snippet

    HtmlPage.Document.AttachEvent("oncontextmenu", OnContextMenu);

    I found a way to invoke this. This is as follows:

    I first check to see if the "AttachEvent" is possible using the below code

    if (HtmlPage.Window.Eval("document.attachEvent") == null)
                    {
                        //attachevent not supported

                      HtmlPage.Window.Invoke("AttachOnContextMenuHandler", new Action(OnContextMenu_EventListener));
                    }

    else

    {

            HtmlPage.Document.AttachEvent("oncontextmenu", OnContextMenu);

    }

    The "AttachOnContextMenuHandler" is javascript function which attaches the 'contextmenu' event

    document.addEventListener('contextmenu', function() { handler(); });

    Regards,

    Vinod

    • Marked as answer by R.Vinod Kumar Friday, October 14, 2016 1:41 PM
    Friday, October 14, 2016 1:41 PM

All replies

  • Hi R.Vinod Kumar,

    According to the attachEvent document, it mentioned that "attachEvent is no longer supported". Please refer to:

    https://msdn.microsoft.com/en-us/library/ms536343%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396

    Starting with Internet Explorer 11, use addEventListener. Please refer the document about addEventListener which contains a sample site about how to use it. Hope it can help you.

    https://msdn.microsoft.com/en-us/library/ff975245(v=vs.85).aspx

    Best Regards,
    Weiwei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, September 30, 2016 7:38 AM
    Moderator
  • Thanks for the reply, Weiwei.

    I had already noted that AttachEvent is no longer supported in IE11. I was looking at a way to call the below code snippet

    HtmlPage.Document.AttachEvent("oncontextmenu", OnContextMenu);

    I found a way to invoke this. This is as follows:

    I first check to see if the "AttachEvent" is possible using the below code

    if (HtmlPage.Window.Eval("document.attachEvent") == null)
                    {
                        //attachevent not supported

                      HtmlPage.Window.Invoke("AttachOnContextMenuHandler", new Action(OnContextMenu_EventListener));
                    }

    else

    {

            HtmlPage.Document.AttachEvent("oncontextmenu", OnContextMenu);

    }

    The "AttachOnContextMenuHandler" is javascript function which attaches the 'contextmenu' event

    document.addEventListener('contextmenu', function() { handler(); });

    Regards,

    Vinod

    • Marked as answer by R.Vinod Kumar Friday, October 14, 2016 1:41 PM
    Friday, October 14, 2016 1:41 PM