window.open被标记为弹出窗口

时间:2017-06-06 04:24:00

标签: javascript jquery api

我试图弄清楚在使用window.open时如何绕过弹出阻止问题。这是我正在处理的代码。

我构建脚本的第一个脚本使用bit.ly API来缩短URL地址。基本上它获取参数然后将其传递给API。

然后第二个脚本调用get_short_url函数,以便通过twitter使用on-click事件共享bit.ly url。不幸的是,window.open似乎被浏览器标记为弹出窗口。有没有办法绕过这个

<script>
var base_url      = window.location.origin,
    hash_bang     = "/#/sign-up?referral=",
    login         = "login",
    api_key       = "api_key";


function get_short_url(login, api_key, func, value) {
    var value = document.getElementById('input-refcode').value;

    $.getJSON(
        "https://api-ssl.bitly.com//v3/shorten?callback=?", 
        { 
            "format": "json",
            "apiKey": api_key,
            "login": login,
            "longUrl": base_url + hash_bang + value
        },

        function(response)
        {
            func(response.data.url);
        }
    );
}
</script>
<script>

$('#twitter').on('click', function() {

    get_short_url(login, api_key, function(short_url) {
        win = window.open('https://twitter.com/intent/tweet?text=Get Free Rides at Electric Studio! Sign up ' + short_url + ' to purchase your first timer package! After your first ride, you get 1 ride on us!' + ' https://www.electricstudio.ph/', '_blank');
        win.focus();
    });
});
</script>

2 个答案:

答案 0 :(得分:0)

我已经在Firefox,Chrome&amp;互联网浏览器。

&#13;
&#13;
var url = 'https://twitter.com/intent/tweet?text='+encodeURIComponent('Get Free Rides at Electric Studio! Sign up xx to purchase your first timer package! After your first ride, you get 1 ride on us! https://www.electricstudio.ph/');
console.log(url);
var win = window.open(url, '_blank');
win.focus();
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我认为你不能绕过弹出警报,因为它是浏览器处理安全的东西。对于您的个人计算机,您可以更改安全设置以将此站点标记为例外,然后不会再次提示您。

对于你想要将ajax调用中的值传递给另一个的第二部分,你可以创建一个如下的对象并将其作为响应传递

function(response)
{
    func({'shortURL' : response.data.url, 'yourValue': customValue });
}

并在您的其他功能中访问此

get_short_url(login, api_key, function(result) {
    var custVal = result.customValue;  //Your custom value
    win = window.open('https://twitter.com/intent/tweet?text=Get Free Rides at Electric Studio! Sign up ' + result.shortURL + ' to purchase your first timer package! After your first ride, you get 1 ride on us!' + ' https://www.electricstudio.ph/', '_blank');
    win.focus();
});