推文按钮打开新窗口和新选项卡

我正在做FreeCodeCamp的Random Quote Machine练习。

使用这个答案 ,我尝试设置我的推文按钮以打开一个新窗口而不是用户可以使用的选项卡来推文。

唯一的问题是它正在打开一个新选项卡以及一个新窗口。 有什么方法可以让它打开新窗口吗?

Tweet链接的HTML

 

相关的JavaScript

 jQuery('a[target^="_newwin"]').click(function() { var width = 500; var height = 300; window.open(this.href , 'newwindow', 'width=' + width + ', height=' + height + ', top=' + ((window.innerHeight - height) / 2) + ', left=' + ((window.innerWidth - width) / 2)); }); 

我的项目的笔

谢谢!

您将获得自定义行为(打开一个新窗口)和链接的标准行为(现在在大多数浏览器中,打开一个新选项卡)。 要防止后者,您需要使用’preventDefault’方法:

 jQuery('a[target^="_newwin"]').click(function(e) { e.preventDefault(); var width = 500; var height = 300; window.open(this.href , 'newwindow', 'width=' + width + ', height=' + height + ', top=' + ((window.innerHeight - height) / 2) + ', left=' + ((window.innerWidth - width) / 2)); });