I am doing FreeCodeCamp's Random Quote Machine exercise.
我正在做FreeCodeCamp的随机报价机练习。
Using this answer, I tried to set up my tweet button to open up a new window rather than a tab that the user could use to Tweet this quotes with.
使用这个答案,我尝试设置我的tweet按钮来打开一个新的窗口,而不是用户可以使用的用于tweet的选项卡。
The only issue is is that it is opening up a new tab as well as a new window. Is there any way I could get it to just open the new window?
唯一的问题是它打开了一个新选项卡和一个新窗口。我能不能让它打开新窗口?
HTML for Tweet Link
HTML的微博链接
<a class="twitter-share-button" href="https://twitter.com/intent/tweet" target="_newwin">
Tweet</a>
Related JavaScript
相关的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));
});
我的笔项目
Thank you!
谢谢你!
1 个解决方案
#1
3
You are getting both your custom behavior (opening a new window) and the standard behavior of a link (in most browser nowadays, opening a new tab). To prevent the latter, you need to use the ´preventDefault´ method:
您将获得自定义行为(打开一个新窗口)和链接的标准行为(在当今大多数浏览器中,打开一个新选项卡)。为了防止后者,需要使用´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));
});
#1
3
You are getting both your custom behavior (opening a new window) and the standard behavior of a link (in most browser nowadays, opening a new tab). To prevent the latter, you need to use the ´preventDefault´ method:
您将获得自定义行为(打开一个新窗口)和链接的标准行为(在当今大多数浏览器中,打开一个新选项卡)。为了防止后者,需要使用´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));
});