简体   繁体   English

关闭当前选项卡并使用 javascript 打开新选项卡

[英]Close the current tab and open new tab using javascript

Close the current tab and open a new tab using JavaScript.关闭当前选项卡并使用 JavaScript 打开一个新选项卡。 On Chrome or any other browser.在 Chrome 或任何其他浏览器上。

Try this:尝试这个:

window.open('http://www.google.com','_blank');
window.setTimeout(function(){this.close();},1000);
  1. Close the current tab关闭当前选项卡

    You can't close the current tab until and unless it has parent reference您不能关闭当前选项卡,除非它有父引用

Its simple, if the current tab is opened by self I mean, you went to browser and typed the url in the url bar can't be closed with the javascript.很简单,如果当前选项卡是由自己打开的,我的意思是,您转到浏览器并在 url 栏中键入 url 无法使用 javascript 关闭。 Because of security reasons browsers will not allow these type of things.由于安全原因,浏览器将不允许这些类型的东西。

If you try the following code如果您尝试以下代码

window.close();

you'll get following warning message by browser您将通过浏览器收到以下警告消息

Scripts may close only the windows that were opened by it.脚本可能只关闭由它打开的窗口。

If the tab is opened with window.open, right click on the link and opened on anchor tag then following should work如果选项卡是用 window.open 打开的,请右键单击链接并在锚标记上打开,那么以下应该可以工作

window.close();
self.close();
  1. Opening new tab using the javascript :使用 javascript 打开新标签:

window.open("https://stackoverflow.com");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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