V24s

"Get Updates : Subscribe to our e-mail newsletter to receive updates........" "Good Luck frnds" Admission 2019 Application Form 2019 Colleges Entrance Exam 2019 Results 2019 Notification 2019 University Educational Jobs 2019 Government Jobs 2019

Search This Blog v24s guys

03 April, 2013

Create Goog.le Short Urls in JavaScript using Google URL Shortener API Example

Introduction

Here I will explain how to create JavaScript short urls using google URL Shortener API in jQuery or create goog.le short urls using jQuery, JavaScript. Google Provides URL Shortener API to create short urls with goog.le for free with a daily limit of 1,000,000 requests.

Description

In previous articles I explained Show user current location on google map, Google Map with latitude and longitude, jQuery Google currency converter and many articles relating to Google API, JavaScript, jQuery,asp.net. Now I will explain how to create short urls using google URL Shortener API in JavaScript , jQuery.

To create short urls using Google api first we need to get API key from Google for that check this articleGet Google API key once you get the api key click on Services >> Enable URL Shortner API then write the following code

<html>
<head>
<title>URL Shortener using Google API. http://goo.gl </title>
<script src="https://apis.google.com/js/client.js" type="text/javascript"> </script>
</head>
<script type="text/javascript">
function makeRequest() {
var Url = document.getElementById("longurl").value;
var request = gapi.client.urlshortener.url.insert({
'resource': {
'longUrl': Url
}
});
request.execute(function(response) {

if (response.id != null) {
str = "<b>Long URL:</b>" + Url + "<br>";
str += "<b>Test Short URL:</b> <a href='" + response.id + "'>" + response.id + "</a><br>";
document.getElementById("result").innerHTML = str;
}
else {
alert("Error: creating short url \n" + response.error);
}
});
}
function load() {
gapi.client.setApiKey('AIzaSyDV5_Ca9cEVSFaiLkyzGIcDcbnV_4CiA0o');
gapi.client.load('urlshortener', 'v1', function() { document.getElementById("result").innerHTML = ""; });
}
window.onload = load;
</script>
<body>
<h2> URL Shortener using Google API. http://goo.gl </h2>
<table>
<tr>
<td>Enter Long URL:</td>
<td>
<input type="text" id="longurl" name="url" size="30" value="http://www.v24s.blogspot.com" />
</td>
</tr>
<tr>
<td></td>
<td><input type="button" value="Create Short Url" onclick="makeRequest();" /></td>
</tr>
<tr>
<td colspan="2">
<div id="result"></div>
</td>
</tr>
</table>
</body>
</html>
If you observe above code in script section I writtengapi.client.setApiKey('AIzaSyDV5_Ca9cEVSFaiLkyzGIcDcbnV_4CiA0o'); here you need to replace with your API Key.

Demo

No comments:

Popular Posts

Recent Posts

Google Analytics