Ad

Auto Click Button On Page Load Is Not Working

- 1 answer

I'm completely newbie to code, and I'm making a website on Webflow and trying to adapt some functions with custom code. I read a bunch of topics here that mentions the possibility of auto clicks a button on load of the page. So I tried adding the snippet to the body of the page and publishing the website to check if it would work, but the auto click didn't happen. The page is only a button with an auto click. Could someone help me to understand why it's not working? Thanks in advance.

<body>
  <div><a id="redirect" ms-member-page="default" target="_blank" rel="nofollow noreferrer" href="#" class="w-button">Button Text</a></div>
  <script src="https://d3e54v103j8qbb.cloudfront.net/js/jquery-3.4.1.min.220afd743d.js?site=5e79526aa1d3ba459f1ebac2" type="text/javascript" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
  <script src="js/webflow.js" type="text/javascript"></script>
  <!-- [if lte IE 9]><script src="https://cdnjs.cloudflare.com/ajax/libs/placeholders/3.0.2/placeholders.min.js"></script><![endif] -->
  <script type="text/javascript">
document.getElementById("redirect").click();
</script>
</body>
Ad

Answer

The autoclick is working, but it's just the fact that your anchor tag (a) is not leading the user anywhere. You probably were trying to do something like this:

<body>
  <div><a id="redirect" ms-member-page="default" target="_blank" rel="nofollow noreferrer" href="/mypage.html" class="w-button">Button Text</a></div>
  <script src="https://d3e54v103j8qbb.cloudfront.net/js/jquery-3.4.1.min.220afd743d.js?site=5e79526aa1d3ba459f1ebac2" type="text/javascript" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
  <script src="js/webflow.js" type="text/javascript"></script>
  <!-- [if lte IE 9]><script src="https://cdnjs.cloudflare.com/ajax/libs/placeholders/3.0.2/placeholders.min.js"></script><![endif] -->
  <script type="text/javascript">
document.getElementById("redirect").click();
</script>
</body>

Read more about anchor tags here.

a tags are for links, to send your user to a page. Buttons are made with the <button> tag.

Ad
source: stackoverflow.com
Ad