Creating Bit.ly Short URLs Using Jquery and Ajax.
💻 coding

Creating Bit.ly Short URLs Using Jquery and Ajax.

1 min read 165 words
1 min read
ShareWhatsAppPost on X
  • 1Creating Bit.ly short URLs can be accomplished with just five lines of client-side code using jQuery and Ajax.
  • 2Users must create a Bit.ly account and modify the username and API key in the script for it to work.
  • 3The provided JavaScript code validates the URL format before sending a request to the Bit.ly API for shortening.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"Creating Bit.ly short URLs can be accomplished with just five lines of client-side code using jQuery and Ajax."

Creating Bit.ly Short URLs Using Jquery and Ajax.

How to create Bit.ly short URLs using Jquery and Ajax. Many tutorials available on the web about short URLs using the server-side script, this script works on the client-side. It is easy just 5 lines of code calling Bit.ly API. You have to modify the username and API key. Use it and make URLs shorts and neat.

Javascript Code

You have to create an account on bit.ly and modify the username and API key.

<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{

//bit_url function
function bit_url(url) 
{ 
var url=url;
var username="username"; // bit.ly username
var key="bit.ly API key";
$.ajax({
url:"http://api.bit.ly/v3/shorten",
data:{longUrl:url,apiKey:key,login:username},
dataType:"jsonp",
success:function(v)
{
var bit_url=v.data.url;
$("#result").html('<a href="'+bit_url+'" target="_blank">'+bit_url+'</a>');
}
});
}

$("#short").click(function()
{
var url=$("#url").val();
var urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
var urltest=urlRegex.test(url);
if(urltest)
{
bit_url(url);
}
else
{
alert("Bad URL");
}
});

});
</script>

//HTML Code
<input type="text" name="url" id="url"/> 
<input type="submit" id="short" value="Submit"/> 
<div id="result"></div>

This javascript code $("#short").click(function(){}- short is the id name of the submit button. Using $("#url").val() calling input box value.

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 19 August 2019 · 1 min read · 165 words

Part of AskGif Blog · coding

You might also like