I'm trying to simulate a mouse click event in IE/FireFox browsers when ctrlKey tag is true. I've googled it a lot, but I cannot find why my code isn't working. What supposed to happen: - triggerClick should click the link (that is fine in both IE & FF) - triggerClickCtrl should open the link in a new tab (in IE - it does the same as the buttom triggerClick, and in FF it does nothing). - triggerClickShift should open the link in a new window (in IE - it does the same as the buttom triggerClick, and in FF it does nothing). Note: this code works in chrome, but it doesn't work in IE11 and FireFox.
any suggestions? Thanks.
<!DOCTYPE html>
<html>
<p><a id = 'bingLink' href = "http://www.bing.com">Bing</a></p>
<button onclick=triggerClick()>triggerClick</button>
<button onclick=triggerClickCtrl()>triggerClickCtrl</button>
<button onclick=triggerClickShift()>triggerClickShift</button>
<script>
function triggerClick()
{
simulateClick(false, false, false);
}
function triggerClickCtrl()
{
simulateClick(true, false, false);
}
function triggerClickShift()
{
simulateClick(false, true, false);
}
var simulateClick = function (ctrl, shift, isMiddle) {
var evt = document.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, window, 0, 0, 0, 10, 10, ctrl, false, shift, false, isMiddle ? 1 : 0, null );
document.getElementById('bingLink').dispatchEvent(evt);
}
</script>
</body>
</html>