Reputation: 1111
I have this API that I'm currently using to output a short URL or URL generator. Is there a way to pass the output to an on-click
function.
The first script shows the API running
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);
}
);
}
Second is a script that shares a text via Twitter using 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();
});
Is there a way to get the output from the first function get_short_url
then bind it in a on-click
like this for example
$('#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();
});
I tried this solution but the browser flagged the function as a pop-up
$('#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();
});
});
Upvotes: 1
Views: 49
Reputation: 1735
I'm no pro at js, but this seems fairly simple. I have not tested this.
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();
}
});
Upvotes: 0
Reputation: 23290
There is no need to, you can simply wrap
$('#twitter').on('click', function() {
get_short_url(login, api_key, function(short_url) {
win = ...
win.focus();
});
});
Upvotes: 1