简体   繁体   中英

How to call function on Safari after page load?

I need to redirect the web page after it loads and gets the param from the URL. I can do this with a button click.

However, I want to redirect the page automatically (without user input so better UX). Here I use window.addEventListener('load', () => handleClick()) and it works well on Chrome, but it does not always fire on Safari (desktop and mobile).

I can debug this by adding alert('Beginning'); in the handler — on Chrome, this fires automatically after the page load, but not on Safari.

How I can solve this?

const handleClick = async (event) => {

  alert('Beginning'); //For debugging


  const stripe = await stripePromise;
  const { error } = await stripe.redirectToCheckout({
    param,
  });
}

if (typeof window !== `undefined`) {
const param = new URLSearchParams(window.location.search).get('param');
}

const Page = () => {

  if (typeof window !== `undefined`) {
  window.addEventListener('load', () => handleClick())
  }

  return (
    <section >

      <button role="link" onClick={handleClick}> //Only for fallback
      Press
    </button>

    </section>
  );
};

export default Page;

无需单击按钮,您就可以使用 history.push 功能。因为它会在被调用后立即触发。

history.push(location)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM