我目前使用此API来输出短网址或网址生成器。有没有办法将输出传递给on-click
函数。
第一个脚本显示正在运行的API
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);
}
);
}
其次是使用on-click
$('#twitter').on('click', function() {
win = window.open('https://twitter.com/intent/tweet?text=Get Free Rides at Electric Studio! Sign up to purchase your first timer package! After your first ride, you get 1 ride on us!', '_blank');
win.focus();
});
有没有办法从第一个函数get_short_url
获取输出,然后将其绑定在on-click
中,例如
$('#twitter').on('click', 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 to purchase your first timer package! After your first ride, you get 1 ride on us!' + short_url, '_blank');
win.focus();
});
我尝试了这个解决方案,但浏览器将该功能标记为弹出式
$('#twitter').on('click', function() {
var base_url = window.location.origin,
hash_bang_twitter = "/%23/sign-up?referral=",
referral_code = document.getElementById('input-refcode').value;
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();
});
});
答案 0 :(得分:1)
没有必要,你可以简单地包装
$('#twitter').on('click', function() {
get_short_url(login, api_key, function(short_url) {
win = ...
win.focus();
});
});
答案 1 :(得分:0)
我不是js的专业人士,但这似乎相当简单。我没有测试过这个。
var base_url = window.location.origin,
hash_bang = "/#/sign-up?referral=",
login = "login",
api_key = "api_key";
function get_short_url() {
var value = ('#input-refcode').val();
var result = '';
$.getJSON(
"https://api-ssl.bitly.com//v3/shorten?callback=?",
{
"format": "json",
"apiKey": api_key,
"login": login,
"longUrl": base_url + hash_bang + value
},
function(response)
{
result = response.data.url;
}
);
return result;
};
$('#twitter').on('click', function() {
var short_url = get_short_url(function(short_url)
if (short_url !== '') {
win = window.open('https://twitter.com/intent/tweet?text=' + encodeURI("Get Free Rides at Electric Studio! Sign up to purchase your first timer package! After your first ride, you get 1 ride on us! " + short_url));
win.focus();
}
});