33
loading...
This website collects cookies to deliver better user experience
// function name is 'shorten'
<button type="submit" class="shorten-button font-2" onclick="shorten()">
Convert
</button>
const shorten = () => {
console.log('button clicked')
}
Axios is a popular, promise-based HTTP client that sports an easy-to-use API and can be used in both the browser and Node.
axios.get('API url')
.then((response) => {
// if the request was Sucessful and no errors were caught
console.log(response)
})
.catch((error) => {
// if errors were there.
console.log(error)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.1/axios.min.js" integrity="sha512-bZS47S7sPOxkjU/4Bt0zrhEtWx0y0CRkhEp8IckzK+ltifIIE9EMIMTuT/mEzoIMewUINruDBIR/jJnbguonqQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
https://api.shrtco.de/v2/shorten?url=YOUR_URL
let longUrl = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals'
axios
.get(`https://api.shrtco.de/v2/shorten?url=${longUrl}`)
.then((responseData) => {
console.log(responseData);
})
.catch((err)=> {
alert('Error check your connectivity or link')
})
let longUrl = document.querySelector("#url-box").value;
// we will also declare a variable for later storage of processed url
let shortUrl;
full_short_link
which is inside result
which is inside of data
.resdata.data.result.full_short_link
to get Processed link inside of then
. Here resdata
is response data from API.axios .get(`https://api.shrtco.de/v2/shorten?url=${longUrl}`)
.then((resdata) => {
//storing Processed URL in already declared variable
shortUrl = resdata.data.result.full_short_link;
console.log(shortUrl)
//check console for Processed URL or errors
})
.catch((err) => {
alert("Error check your connectivity or link");
});
.innerHtml
property, We will pass this Markup (as a string) and pass variables (longUrl
, shortUrl
) using template strings:` <div class="item">
<div class="long-url">
<div class="title font-1">
Long URL
</div>
<button id="long-url" class="font-2">
${longUrl}
</button>
</div>
<div class="short-url">
<div class="title font-1">
Short URL
</div>
// providing shortUrl variable in value attribute for the copy function⏬
<button id="short-url" class="font-2" value='${shortUrl}' onclick='copyLink(this)'>
${shortUrl}
</button>
</div>
</div> `
// compressed version (spaces and tabs removed) because JS will
// give errors on 'tab' and 'enter'
<div class="item"><div class="long-url"><div class="title font-1">Long URL</div> <button id="long-url" class="font-2"> ${longUrl} </button></div><div class="short-url"><div class="title font-1">Short URL</div> <button id="short-url" class="font-2 hint--bottom" aria-label="Click link to Copy" onclick='copy(this)' value='${shortUrl}' onclick='copyLink(this)'>${shortUrl}</button></div></div>
//Final working:
function addResult(longUrl, shortUrl) {
let parentEle = document.querySelector("#shortened-links");
let data = `<div class="item"><div class="long-url"><div class="title font-1">Long URL</div> <button id="long-url" class="font-2"> ${longUrl} </button></div><div class="short-url"><div class="title font-1">Short URL</div> <button id="short-url" class="font-2" value='${shortUrl}' onclick='copyLink(this)'>${shortUrl}</button></div></div>`;
let newEle = document.createElement("div");
newEle.innerHTML = data;
parentEle.appendChild(newEle);
}
longUrl
and shortUrl
as parameters.then
method, which will execute when data is Fetched from API:.then((resdata) => {
shortUrl = resdata.data.result.full_short_link;
addResult(longUrl, shortUrl);
})
item
will show up on the screen.function copyLink(tag){
let text = tag.value;
// copy link to clipboard
navigator.clipboard.writeText(text);
}
this
keyword. for ex:// usage of this function:
onclick='copyLink(this)'
this
keyword will pass the full button tag through an argument so that we will later be able to use value property in CopyLink func.shortUrl
tag. // we can do something like this:
if (longUrl){
//if their is value in input tag then only Axios request is sent
axios.get()....
}else{
alert('Enter some data first!!')
}