Here is the plunkr: https://plnkr.co/edit/zhWBY7G0ALnM8dBSZ5nm?p=preview
If you change that 1001
to 1000
, the dispatch will work as expected - the link will be followed. The event handler always gets invoked as expected. It's just the default behavior (following the link) stops working. I googled around for a reason to no avail.
JS:
window.onload = function(){
var trigger = document.getElementById('trigger');
trigger.addEventListener('click', clickLink);
var element = document.getElementsByTagName('a')[0];
element.addEventListener('click', function(){
// this will work regardless...
console.log('link clicked!')
});
function clickLink(){
window.setTimeout(function(){
var evt = document.createEvent("MouseEvents");
evt.initEvent("click", true, true);
element.dispatchEvent(evt);
}, 1001);
}
}
HTML:
<a href="http://google.com" target="_blank">Google</a>
<span id="trigger">Click Me</span>
This is a simplified version of an angular example in which i try to implement a timeout before following a link in a directive. Same issue. https://plnkr.co/edit/yGBp6PkvpbQkQUTeHX40?p=preview
What is happening? Why the magical 1000 ms cutoff? I'm on chrome.