Deepak Kamat
Deepak Kamat

Reputation: 1980

How to automatically click a link when the website loads

I am trying to figure out how to make an anchor link clicked when the website is visited. I would be grateful if someone can help me, here's a demo code, you can match the id for my ease thank you.

<body>
<a id='deepakkamat' href='http://mywebpage.com'> This is my webpage </a>
</body>

So i want to know any JavaScript code to make the link clicked when the page loads. Thank you in advance.

Upvotes: 2

Views: 21039

Answers (3)

Joel Etherton
Joel Etherton

Reputation: 37533

The jquery answer:

$($('#deepakkamat').click());

This will actually register a click event as the cause, but it seems awfully unnecessary. The meta method is the preferred method.

Upvotes: 2

fb55
fb55

Reputation: 1227

When you're trying to archive a redirect, use document.location.href = "http://mywebpage.com" (or the meta tag mentioned before).

There is also a click method available, so that you can run

document.getElementById("deepakkamat").click()​​​;​

Upvotes: 0

woz
woz

Reputation: 10994

Use the meta tag to redirect to another page when the page loads:

<meta http-equiv="refresh" content="2;url=http://www.example.com/" />

Where 2 is the number of seconds before the redirect occurs.

This will be more reliable and more straightforward than writing JavaScript to click a link. Let me know how it works for you.

Upvotes: 3

Related Questions