在不刷新引用页面的情况下以编程方式打开新选项卡?
Opening new tab programmatically without refreshing referring page?
我通过 POST 请求加载了一个页面,该请求以编程方式打开另一个选项卡,如下所示:
Object.assign(document.createElement("a"),
{ target: "_blank", href: "foo.html"}).click();
然而,发生的情况是,除了打开新选项卡外,它还刷新了原始页面,但它使用的是 GET 请求而不是 POST 请求,这意味着我失去了所有POST 参数发送到原始页面。
有什么方法可以在不刷新引用页面的情况下打开新标签页吗?
您可以为此使用 window.open
API (https://developer.mozilla.org/en-US/docs/Web/API/Window/open)。就这样称呼它 window.open('foo.html', '_blank');
正如基思之前所写:
The current code you are showing won't cause the current page to refresh,. I'm assuming your running this code on a Form, it's the form submitting that causes the refresh not this. You can use event.preventDefault on your form to stop that.
这确实是我的问题,调用 event.preventDefault() 解决了它。谢谢,基思!
我通过 POST 请求加载了一个页面,该请求以编程方式打开另一个选项卡,如下所示:
Object.assign(document.createElement("a"),
{ target: "_blank", href: "foo.html"}).click();
然而,发生的情况是,除了打开新选项卡外,它还刷新了原始页面,但它使用的是 GET 请求而不是 POST 请求,这意味着我失去了所有POST 参数发送到原始页面。
有什么方法可以在不刷新引用页面的情况下打开新标签页吗?
您可以为此使用 window.open
API (https://developer.mozilla.org/en-US/docs/Web/API/Window/open)。就这样称呼它 window.open('foo.html', '_blank');
正如基思之前所写:
The current code you are showing won't cause the current page to refresh,. I'm assuming your running this code on a Form, it's the form submitting that causes the refresh not this. You can use event.preventDefault on your form to stop that.
这确实是我的问题,调用 event.preventDefault() 解决了它。谢谢,基思!