[英]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.我需要在网页加载并从 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).在这里,我使用window.addEventListener('load', () => handleClick())
并且它在 Chrome 上运行良好,但它并不总是在 Safari(桌面和移动)上触发。
I can debug this by adding alert('Beginning');
我可以通过添加alert('Beginning');
来调试它alert('Beginning');
in the handler — on Chrome, this fires automatically after the page load, but not on Safari.在处理程序中 - 在 Chrome 上,它会在页面加载后自动触发,但在 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.