gpt4 book ai didi

javascript - 页面无法重定向并清除 URL 参数

转载 作者:行者123 更新时间:2023-12-03 04:04:01 25 4
gpt4 key购买 nike

我有一个向 PHP 脚本发出 POST 请求的网页。根据请求的结果,按钮的 onclick 事件设置重定向到两个页面之一。

<button id="button" type="submit">Proceed</button>

...

$.post('script.php', {
key: value
}, function(result) {
if (result != null) {
document.getElementById("button").onclick = function() {
window.top.location.href = "https://example.com/page?otherkey=othervalue";
}
} else {
document.getElementById("button").onclick = function() {
window.top.location.href = "https://example.com/otherpage?otherkey=othervalue";
};
}
});

这在桌面浏览器上运行良好,但在 iOS 上的 Safari 上(特别是在 iOS 10.3.2 上测试),单击按钮后,页面会刷新并且不会重定向到正确的站点。此外,它还会清除以前存在的所有 URL 参数。例如,如果带有按钮的页面是 example.com/page?key=value,该页面将刷新并变为 example.com/page?#_=_ 。我尝试过调试和检查 Javascript 控制台,但它什么也没说。

重定向是我自己域中的页面,尽管带有按钮的页面已集成到 Facebook 应用程序页面(如果相关)。

此外,如果我自己构建 URL 并尝试正常访问它,它会正常加载。我不知道是什么原因导致这种情况,所以我不确定是否还有其他值得发布的相关信息,但如果有必要我可以说更多。

最佳答案

Safari 不能很好地处理函数中返回 false 的情况,尤其是根本不返回的情况。我会在 html 元素中包含一个 onsubmit="return function();" ,我猜这是一个表单。您还可以通过 $('[form ID]').submit(function(){ do something here; return false;});

将其附加到 Submit() 事件监听器

关于javascript - 页面无法重定向并清除 URL 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44639925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com