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.

karel
  • 5,489
  • 46
  • 45
  • 50
Allie
  • 1
  • 1
  • For IE: [“Arrow function” not working in IE, why?](https://stackoverflow.com/questions/40216015/arrow-function-not-working-in-ie-why) – Ivar Jan 16 '20 at 16:03
  • ie doesn't support arrow functions not sure about ff. Try `setTimeout(function() { window.location = 'thankyou.html' },2500)` – Pete Jan 16 '20 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);"` – Ivar Jan 16 '20 at 16:08

1 Answers1

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>
Nino Filiu
  • 16,660
  • 11
  • 54
  • 84
  • Thanks everybody! This did it! :) – Allie Jan 16 '20 at 16:21
  • 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): – Allie Jan 16 '20 at 16:43
  • 1
    @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. – Nino Filiu Jan 16 '20 at 16:55