最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to automatically click a link when the website loads - Stack Overflow

programmeradmin3浏览0评论

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=''> 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.

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.'> 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.

Share Improve this question asked May 30, 2012 at 13:14 Deepak KamatDeepak Kamat 1,9805 gold badges24 silver badges41 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

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

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

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.

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.

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

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

document.getElementById("deepakkamat").click()​​​;​
发布评论

评论列表(0)

  1. 暂无评论