Emulate a click on a link with Javascript that works with IE

I want the java script to click the link on the page .. I found something on the net that suggests adding such a function:

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'); 

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

Any ideas?

+22
javascript firefox internet-explorer
May 6 '09 at 2:06 a.m.
source share
5 answers

It seems to me that you still need to call document.createEventObject - you just checked it there. Unfulfilled code follows, but based on 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 ); } } 
+36
May 6 '09 at 2:37 a.m.
source share

At first, this did not work for me, and then I saw that there was no parameter in the code for part of IE. Here is 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); } } 
+2
Dec 08 '09 at 23:59
source share

Try it if you still get the error (using 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); } } 
+1
May 17 '12 at 9:47 a.m.
source share

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

 function clickLink(id){ location.href=document.getElementById(id).href; } 
0
Jun 10 '10 at 17:54
source share

We found an easier way to simulate a right click (tested in IE8). Use a double click or two clicks, then right-click using Shift-F10. I don’t know exactly why this works, but it is. In the following code example, we use the Selenium method to double-click, then use the IE driver to find WebElement and send the Shift-F10 key sequence that emulates a right-click. We use this to test GWT-based web applications. In one place, this did not work, it was in the control tree, where the context menu was set to appear in the coordinates of the mouse. Often the coordinates of the mouse were negative, so right-clicking on a menu item did not display the parameters of the child menu. To handle this case, we added some code to the control, so that if the mouse coordinates were negative, the context menu is displayed at 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); 
0
Jun 15 2018-10-15T00:
source share



All Articles