鸣叫按钮打开新窗口和新选项卡 [英] Tweet button opening new window and new tab

查看:104
本文介绍了鸣叫按钮打开新窗口和新选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做FreeCodeCamp的随机报价机练习。

I am doing FreeCodeCamp's Random Quote Machine exercise.

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

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.

唯一的问题是它正在打开一个新标签以及一个新窗口。有没有什么方法可以让它打开新窗口?

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

<a class="twitter-share-button" href="https://twitter.com/intent/tweet" target="_newwin">
Tweet</a>

相关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'方法:

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:

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));
});

这篇关于鸣叫按钮打开新窗口和新选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆