gpt4 book ai didi

javascript - ajax调用成功后如何关闭浏览器中当前打开的选项卡

转载 作者:太空宇宙 更新时间:2023-11-04 16:21:28 25 4
gpt4 key购买 nike

下面是我的ajax调用,一旦ajax调用成功,我想从浏览器关闭当前选项卡。

$.ajax({
type: "GET",
data: null,
url: '@Url.Action("SessionKill", "Authentication")',
dataType: 'json',
contentType: false,
processData: false,
success: function (response) {
if (response != null && response.success) {
window.location = window.close();//from here,I want to close the tab.
}
},
error: function (response) {
}
});

最佳答案

你有一个错字。试试这个:

element.close();

代替:

window.location = window.close();

window.close can only close windows opened with window.open so you can't close the current tab in javascript. You can open a new window, store it in a variable, and then close it. – Shaun Parsons

用于打开窗口:

var wind;

function openWindow() {
wind = window.open("https://www.google.com/", "wind", "width=1000,height=700");
}

function closeWindow() {
wind.close();
}
<input type="button" onclick="openWindow()" value='Open'/>
<input type="button" onclick="closeWindow()" value='Close'/>

更新

此代码段在堆栈溢出时被阻止,因此不会在此处执行。

<强> Why is window.open() blocked?

关于javascript - ajax调用成功后如何关闭浏览器中当前打开的选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40605621/

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