0

I am trying to use inline javascript to set a timeout for a link I am calling on my website:

<a href="javascript:setTimeout(()=>{window.location = 'thankyou.html' },2500);" class="burst_1 btn">Elementary School Camp</a>

It works in Chrome but not in Firefox or Internet Explorer. I tried to search online for alternatives but nothing has worked for me so far. I need a timeout because the buttons I am using on my site have a fireworks effect and I'd like visitors to see the effect for a few seconds before they get sent to the link page.

Or is there a better way to achieve what I am trying to do? Open to suggestions.

3
  • For IE: “Arrow function” not working in IE, why? Commented Jan 16, 2020 at 16:03
  • ie doesn't support arrow functions not sure about ff. Try setTimeout(function() { window.location = 'thankyou.html' },2500) Commented Jan 16, 2020 at 16:03
  • For Firefox, you should add void before the setTimeout so the return value of setTimeout isn't redirected to. So this should work: href="javascript: void setTimeout(function () {window.location = 'thankyou.html' },2500);" Commented Jan 16, 2020 at 16:08

1 Answer 1

2

Replace <a href="javascript:..."> by <a href="#" onclick="...">:

function waitThenNavigateTo (newLocation) {
  setTimeout(() => {
    window.location = newLocation;
  }, 1000);
}
<a href="#" onclick="waitThenNavigateTo('http://example.com')">Click me!</a>

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks everybody! This did it! :)
One more question. For IE, I went to the Babel link to transform my code. This is what I got for Nino's code (but someone told me it's not working on IE): <script>function waitThenNavigateTo (newLocation) { setTimeout(function(){ window.location = newLocation; }, 3000); }</script>
@Allie indeed, arrow functions are not supported by IE, so Babel translates them into classic functions and handles the related context changes that are implied. The code transformed by babel should work on IE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.