How do I make jquery click test
You need to trigger the default click
method, not the one by jQuery. This can be done by adding the default click option within a click event of jQuery using this
.
<a href="http://about.com/"></a>
This is how the JavaScript looks. It basically creates the event when the DOM is ready, and clicks it intermediately, thus following the link.
$(function() {
$('a').click(function() {
// 'this' is not a jQuery object, so it will use
// the default click() function
this.click();
}).click();
});
To see a live example (opening about.com), see: http://jsfiddle.net/8H9UX/
document.getElementById('mylink').click();
trigger('click')
will fire the click event but not the default one.
$('a').click(function(){ alert('triggered') }) // this will be fired by trigger
$(document).ready(function(){
$('#mylink').trigger('click');
});
If you are expecting the file to get downloaded, it will not happen becauer trigger() will not trigger the default event.
use the following way.... since you want to download the file prevent the link from navigating.
$(document).ready(function() {
$('#mylink').click(function(e) {
e.preventDefault(); //stop the browser navigating
window.location.href = 'test.zip';
});
});
You need to wait until the DOM has finished loading. This can be done with jQuery. The anonymous function is run at page load once all the elements are available in the DOM.
<script>
$(function() {
$('#mylink').trigger('click');
});
</script>