Friday, October 3, 2008

Fire (Invoke) Java Script Click Event Programmically on FF and IE

For this create one button




input id="btn" value="Click 1" onclick="alert('hi');" type="button"
input id="btn1" value="Click 2" onclick="fire" type="button"



function fire() {
var menulink = document.getElementById('btn');
if (document.createEvent) { // FF model
var customClick = document.createEvent('MouseEvents');
customClick.initEvent('click',0,0);
mailLink.dispatchEvent(customClick);
// The old good click() is removed from link methods:
try { mailLink.click(); } catch(e){alert(e.toString());}
}
// *********************************
// IE accepts programmed events,
// but default action will be taken only
// from hardvare device input (like FF/NN):
else if (document.createEventObject) { // IE model
var customClick = document.createEventObject();
mailLink.fireEvent('onclick', customClick);
// The old good click() was simply forgotten
// by the brave IE team and allows to bypass security:
mailLink.click(); // equals to a hardware click
}
}

Contributors