Abhishek
Abhishek

Reputation: 7045

How to simulate click on page load?

My objective is not simple redirection!

Even before you mark it as duplicate, I've already tried this, this and this. It didn't worked. I've tried it in following code.

<a href="http://google.com" class="test_class">click me</a>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script language="javascript">
$("document").ready(function() {
    setTimeout(function() {
        //$('.test_class').click();
        $('.test_class').trigger('click');
    },10);

});
</script>

I've tried the click event with and without setTimeout, nothing worked. I've tried with id as well, it didn't worked. I'm using google chrome Version 44.0.2403.157 (64-bit) on ubuntu 14.04, if at all it matters.

Edit: I've tried following variations as well just now, and they didn't worked :(

 $(document).ready(function() { //removed quotes.
    setTimeout(function() {
        //$('.test_class').click();
        $('.test_class').trigger('click');
    },10);

});

This one

$(document).ready(function() {
    setTimeout(function() {
        //$('.test_class').click();
        $('.test_class')[0].trigger('click'); //added array representation
    },10);

});

And This one, Peculiarity of this click event is that I can see alert, but the click event of <a> isn't happening.

<a href="http://google.com" class="test_class">click me</a>
<div class="submit_btn" style="display:none;" onclick="dothis();"></div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script language="javascript">
function dothis()
{
    alert('dskjhfjkds');
    $('.test_class').click();
}
$(document).ready(function() {
    setTimeout(function() {
        //$('.test_class').click();
        $('.submit_btn').click();
    },10);

});

//$('.test_class')[0].trigger('click');
</script>

p.s. My actual scenario of of click event differs, it is not simple redirection to google website.

Upvotes: 1

Views: 3165

Answers (3)

D4V1D
D4V1D

Reputation: 5849

It seems that href attribute is not followed when you trigger the click event on a <a> element.

Therefore:

<a href="http://google.com" class="test_class">click me</a>
<script>
    $(document).ready(function() {
        setTimeout(function() {
            $('.test_class').click(); // this won't trigger the link to google.com
        },10);
    });
</script>

Won't have any effect when:

<a href="http://google.com" onClick="alert('this is an alert');" class="test_class">click me</a> <!-- notice the onClick attribute -->
<script>
    $(document).ready(function() {
        setTimeout(function() {
            $('.test_class').click(); // this will trigger the alert
        },10);
    });
</script>

This one will.

I guess there must be something preventing links with href attribute to be triggered by Javascript.

If you want to perform a redirect in HTML, I suggest you to dig into the <meta http-equiv="refresh" content="5; URL=http://www.mydomain.tld"> tag.

Upvotes: 2

Spencer Wieczorek
Spencer Wieczorek

Reputation: 21575

You need to do the DOM event click for the element by accessing $('.test_class')[0]:

$('.test_class')[0].click();

Upvotes: 1

Robin Carlo Catacutan
Robin Carlo Catacutan

Reputation: 13679

Do this instead:

$('.test_class')[0].click();

Upvotes: 2

Related Questions