2

I'm using Windows 8, Internet Explorer 10, Visual Studio 2013

Here is javascript code :

function simulate(element, eventName)
{
    var options = extend(defaultOptions, arguments[2] || {});
    var oEvent, eventType = null;

    for (var name in eventMatchers)
    {
        if (eventMatchers[name].test(eventName)) { eventType = name; break; }
    }

    if (!eventType)
        throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported');

    if (true || document.createEvent) // temporary it's always true to check IE
    {
        oEvent = document.createEvent(eventType);
        if (eventType == 'HTMLEvents')
        {
            oEvent.initEvent(eventName, options.bubbles, options.cancelable);
        }
        else
        {
            oEvent.initMouseEvent(eventName, options.bubbles, options.cancelable, document.defaultView,
            options.button, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
            options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, element);
        }
        element.dispatchEvent(oEvent);
    }
    else
    {
        options.clientX = options.pointerX;
        options.clientY = options.pointerY;
        var evt = document.createEventObject();
        oEvent = extend(evt, options);
        element.fireEvent('on' + eventName, oEvent);
    }
    return element;
}

function extend(destination, source) {
    for (var property in source)
      destination[property] = source[property];
    return destination;
}

var eventMatchers = {
    'HTMLEvents': /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,
    'MouseEvents': /^(?:click|dblclick|mouse(?:down|up|over|move|out))$/
}
var defaultOptions = {
    pointerX: 0,
    pointerY: 0,
    button: 0,
    ctrlKey: false,
    altKey: false,
    shiftKey: false,
    metaKey: false,
    bubbles: true,
    cancelable: true
};

My HTML :

<a href="http://google.com" id="rrx"  > google </a>

And here is my javascript code that calls simulate :

simulate(document.getElementById('rrx'), "click"); 

The problem is that I'm not redirected to google web page because IE throws a exception with message that document.createEvent is not supported. As far as I know document.createEvent should be supported with IE9+.

noseratio
  • 56,401
  • 21
  • 172
  • 421
Mohsen Sarkar
  • 5,562
  • 7
  • 41
  • 83
  • You could use jQuery to do this more easily. Do You definitely want to use pure javascript? – Skriptotajs Feb 19 '14 at 18:41
  • `document.createEvent` works in my IE9. Have you checked that your webpage or server isn't forcing a browser-compatability mode to IE8 or 7? – towr Feb 19 '14 at 19:20
  • @Skriptotajs I have tried it with no success. Please check my other question http://stackoverflow.com/questions/21887238/why-jquery-trigger-or-triggerhandler-doesnt-work – Mohsen Sarkar Feb 19 '14 at 19:22
  • @towr Actually I'm using .NET WebBrowser control! – Mohsen Sarkar Feb 19 '14 at 19:24
  • It seems it might very well use an old version of IE, see http://stackoverflow.com/questions/1786905/c-sharp-web-browser-component-is-ie7-not-ie8-how-to-change-this, http://stackoverflow.com/a/14667763/1961666 (check `Response.Write(Request.Browser.Version.ToString());` to see if this applies in your case ) – towr Feb 19 '14 at 19:30
  • @towr I used to set that registery key to 9999. And now whenever I check webbrowser version the page returns that I'm using Internet Explorer 10.However still problem exist and isn't solved. – Mohsen Sarkar Feb 19 '14 at 20:11

1 Answers1

2

Add an alert to show document.documentMode and document.compatMode, what do you see?

I believe you should have both the correct FEATURE_BROWSER_EMULATION value in the registry and <!DOCTYPE html> in the HTML itself, for all modern HTML DOM/JavaScript features to work.

This also applies to the related question of yours.

Community
  • 1
  • 1
noseratio
  • 56,401
  • 21
  • 172
  • 421