Ok, so I searched all over with no answer.
Can someone explain why
does not work with .trigger('click')
<a id="openNew" href="http://www.example.org">Click me</a>
<script type='text/javascript'>
$(window).load(function(){
$('#openNew').addClass("external").attr({ target: "_blank", href: 'http://www.google.com' }).trigger('click');
});
</script>
and it does not work with .click()
<script type='text/javascript'>
$(window).load(function(){
$('#openNew').addClass("external").attr({ target: "_blank", href: 'http://www.google.com' }).click();
});
</script>
Does not click the link whatever I do. It only works if I click it. How can I make it auto click?
Working on this for about 1 hour and is driving me crazy, I know I’m must be doing something stupid.
JsFiddle for your convenience.
I wouldn’t mind any other solution in plain JavaScript.
Simulating a user physically clicking the link is not possible. Since you are using target=’_blank’ I presume you want a new window? So you’ll need to use window.open. Which popup blockers wont like.