Skip to content

Instantly share code, notes, and snippets.

@chongiscool
Created April 24, 2018 03:46
Show Gist options
  • Save chongiscool/f36fe24c5fdaf5061c7c0880ec066c41 to your computer and use it in GitHub Desktop.
Save chongiscool/f36fe24c5fdaf5061c7c0880ec066c41 to your computer and use it in GitHub Desktop.
JS 'get' request temp for expand url from google shorter url
// Include data for accessing Google APIs
const apiKey = 'js-request-api-key-from-google';
const projection = 'FULL';
const url = 'https://www.googleapis.com/urlshortener/v1/url';
// Some page elements
const $inputField = $('#input');
const $expandButton = $('#expand');
const $shortenButton = $('#shorten');
const $responseField = $('#responseField');
// AJAX functions
function expandUrl() {
const urlToExpand = url + '?key=' + apiKey +
'&shortUrl=' + $inputField.val();
const xhr = new XMLHttpRequest();
xhr.responseType = 'json';
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
console.log(xhr.response);
$responseField.append('<p>Your shortend url is: </p><p>' + xhr.response.id + '</p>');
$responseField.append('<p>Your expanded url is: </p><p>' + xhr.response.longUrl + '</p>');
}
}
xhr.open('GET', urlToExpand);
xhr.send();
}
function shortenUrl() {
}
function expand() {
$responseField.empty();
expandUrl();
return false;
}
function shorten() {
$responseField.empty();
shortenUrl();
return false;
}
// Call functions on submit
$expandButton.click(expand);
$shortenButton.click(shorten);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment