Here's the function that is called when the user clicks a button that generates a shortened url and I'm wondering where I would use an AJAX call for the throbber. It takes a few seconds and I wanted to use a throbber in the mean time. Or would I end up creating a whole new function outside of this one that is called when the button is clicked? I haven't seen any other examples that look like my code. Really new here, would really appreciate any help, thanks.
html that I suppose I would need to link the gif with...
<img src="gif/Loader.gif" id="loader"/>
current js function I'm using to generate the short url function, it takes a couple seconds though so I think I need to call my gif in there somewhere and get rid of it when the callback is successful?
function getShare()
{
var s = document.createElement('script');
var browserUrl = document.location.href;
//alert(browserUrl);
if (browserUrl.indexOf("?") != -1){
browserUrl = browserUrl.split("?");
browserUrl = browserUrl[0];
}
//alert(browserUrl);
var gifUrl = document.getElementById('gif_input').value;
var vidUrl = document.getElementById('vid_input').value;
//alert(gifUrl + "|" + vidUrl);
url = encodeURIComponent(browserUrl + "?gifVid=" + gifUrl + "|" + vidUrl);
//alert(encodeURIComponent("&"));
s.id = 'dynScript';
s.type='text/javascript';
s.src = "http://b1t.co/Site/api/External/MakeUrlWithGet?callback=resultsCallBack&url=" + url;
document.getElementsByTagName('head')[0].appendChild(s);
}
function resultsCallBack(data)
{
//document.getElementById("results").innerHTML = JSON.stringify(data);
//alert(JSON.stringify(data));
var obj = jQuery.parseJSON(JSON.stringify(data));
//alert(obj.shortUrl);
jQuery("#input-url").val(obj.shortUrl);
}
Add throbber before appending
script
element to theDOM
and remove it as first line inresultsCallback
.If you want handle error states as well, take a look at How to tell if a <script> tag failed to load.