Reputation: 39
I want to open a hyperlink (which contains some 3rd party app) on page load.
I am not sure how I can load it in page without clicking it. So I have to fake the onclick
event. Please help.
I tried using Jquery :
$( "#id_1" ).trigger( "click" );
But its not working.
Upvotes: 0
Views: 174
Reputation: 74738
As a simple answer to this is, It won't be triggered because the element does not seem to have a jQuery event bound to it.
What your code says:
$( "#id_1" ).trigger( "click" );
It says that trigger the jQuery click
event which is bound to #id_1
So either you mock it up
$("#id_1").on("click", function(e){
e.preventDefault();
window.location.href = this.getAttribute('href');
}).trigger('click'); // this gets triggered on page load.
Upvotes: 0
Reputation: 318252
trigger()
triggers jQuery event handlers, not the native functionality of an anchor? What you're probably looking for is
window.location = $('#id_1').attr('href');
Not sure why you would want to do that on pageload, but the point is that you have to manually fake the default functionality of the anchor.
Also, depending on what the anchor really does, you could try the native click() method and see if that does what you want.
document.getElementById('id_1').click()
Upvotes: 1