22

I want to have java script clicking a link on the page..I found something on the net that suggests adding a function like this:

function fireEvent(obj,evt){

    var fireOnThis = obj;
    if( document.createEvent ) {
      var evObj = document.createEvent('MouseEvents');
      evObj.initEvent( evt, true, false );
      fireOnThis.dispatchEvent(evObj);
    } else if( document.createEventObject ) {
      fireOnThis.fireEvent('on'+evt);
    }
}

Then call it using:

fireEvent(document.getElementById('edit_client_link'),'click');

This seems to work fine for FF but with IE it doesn't work!

Any ideas?

Tam
  • 11,872
  • 19
  • 69
  • 119

5 Answers5

36

I think you still need to call document.createEventObject -- you only checked that it's there. Untested code follows, but based on the docs it should work.

function fireEvent(obj,evt){

    var fireOnThis = obj;
    if( document.createEvent ) {
      var evObj = document.createEvent('MouseEvents');
      evObj.initEvent( evt, true, false );
      fireOnThis.dispatchEvent( evObj );

    } else if( document.createEventObject ) {
      var evObj = document.createEventObject();
      fireOnThis.fireEvent( 'on' + evt, evObj );
    }
}
lsl
  • 4,371
  • 3
  • 39
  • 54
Anonymous
  • 49,213
  • 1
  • 25
  • 19
  • 17
    I went ahead and tested this myself in IE 7 and it works; nice job! I loathe the fact that everyone and their mom always jumps on the framework bandwagon when someone simply asked a JavaScript question. Yes, we know, we know, jQuery can do everything AND make dinner for you, but sometimes it's nice to know how things work without such a library. (I use both jQuery and MochiKit, but I still enjoy plain ol' JavaScript). – Jason Bunting May 06 '09 at 04:33
  • 1
    Wait, this doesn't make sense. You're calling `document.createEventObject()`, but not doing anything with the event it returns. How is this different from the code in the question, and why does it (supposedly) work? I'm tempted to think that Jason changed something else that made his code start working. – s4y Jun 22 '10 at 16:18
  • @Sid, you are correct about Jason changing something. See my revision for details if you need to. – lsl Jan 25 '11 at 04:10
  • 1
    i'm sorry but the fallback for IE isn't working for me. it stops when i call fireEvent on fireOnThis. – André Alçada Padez Aug 14 '11 at 19:03
  • nice! just one remark, in order to make it work in IE7 I had to surround document.createEvent and document.createEventObject on trycatch. – damian Feb 07 '12 at 13:07
  • 2
    This doesn't seem to work if you're dynamically creating an element, at least for IE8. – kamranicus Sep 13 '13 at 18:25
  • What can you do if you need SHIFT-CLICK eg. "e.shiftKey" + "e.click" at the same time ? – dza Nov 09 '13 at 17:52
2

This didn't work for me at first and then I saw the code is missing a parameter for the IE portion. Here's an update that should work:

function fireEvent(obj, evt) {
    var fireOnThis = obj;

    if (document.createEvent) {
        // alert("FF");
        var evtObj = document.createEvent('MouseEvents');
        evtObj.initEvent(evt, true, false);
        fireOnThis.dispatchEvent(evtObj);
    } 

    else if (document.createEventObject) {
        // alert("IE");
        var evtObj = document.createEventObject();
        fireOnThis.fireEvent('on'+evt, evtObj);
    }
}
LoveAndCoding
  • 7,857
  • 2
  • 31
  • 55
1

Try this if you are still getting error (Use of Jquery + prototype)

function fireEvent(element,event){  
if (document.createEventObject){
// dispatch for IE
try {
      var evt = document.createEventObject();
      jQuery(element).change();
      return element.fireEvent('on'+event,evt);
} catch(e) { }                  
}
else{
// dispatch for firefox + others
var evt = document.createEvent("HTMLEvents");
evt.initEvent(event, true, true ); // event type,bubbling,cancelable
return !element.dispatchEvent(evt);
}
}
Disha
  • 11
  • 1
0

If you want to simulate clicks only for links, you can use this:

function clickLink(id){
location.href=document.getElementById(id).href;
}
molnarm
  • 9,856
  • 2
  • 42
  • 60
0

We found a simpler way to simulate right click (tested in IE8). Use a double click or two single clicks, then right-click using Shift-F10. I don't know exactly why this works, but it does. In the sample code below, we use the Selenium method to click twice, then use the IE Driver to find the WebElement and send the key sequence Shift-F10, which emulates the right-mouse button click. We use this to testing GWT based web apps. One place this did not work as well was in a tree control where the context menu was set to appear at the mouse's coordinates. Often, the mouse coordinates were negative, so right-clicking the menu item did not cause the child menu options to get displayed. To handle this case, we added a bit of code to the control so that if the mouse coordinates were negative, the context menu appears at a 0,0.

selenium.click("//td[@role='menuitem' and contains(text(), 'Add')]");
selenium.click("//td[@role='menuitem' and contains(text(), 'Add')]");
new InternetExplorerDriver().findElement(By.xpath("//td[@role='menuitem' and contains(text(), 'Add')]")).sendKeys(Keys.SHIFT, Keys.F10);
Rich
  • 1
  • 1