Auto Click Button On Page Load Is Not Working
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>
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.
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM