Responsive URL Shortener
URL Shortener Tool
Enter a long URL to get a short one:
body {
font-family: Arial, sans-serif;
background: linear-gradient(135deg, #6a11cb, #2575fc);
color: #fff;
margin: 0;
padding: 0;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.container {
background: rgba(255, 255, 255, 0.1);
padding: 20px;
border-radius: 10px;
text-align: center;
width: 90%;
max-width: 400px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
h1 {
margin: 0 0 20px;
font-size: 2rem;
}
p {
margin: 10px 0;
}
form {
display: flex;
flex-direction: column;
gap: 10px;
}
input[type="url"] {
padding: 10px;
border: none;
border-radius: 5px;
font-size: 1rem;
}
button {
padding: 10px;
border: none;
border-radius: 5px;
background-color: #2575fc;
color: #fff;
font-size: 1rem;
cursor: pointer;
transition: background 0.3s;
}
button:hover {
background-color: #6a11cb;
}
.hidden {
display: none;
}
#error {
color: #ff6b6b;
font-size: 0.9rem;
}
#result {
margin-top: 20px;
}
document.getElementById('urlForm').addEventListener('submit', async function (e) {
e.preventDefault();
const longUrl = document.getElementById('longUrl').value;
const shortenedUrlElement = document.getElementById('shortenedUrl');
const copyButton = document.getElementById('copyButton');
const errorElement = document.getElementById('error');
shortenedUrlElement.textContent = '';
copyButton.classList.add('hidden');
errorElement.classList.add('hidden');
if (!longUrl) {
errorElement.textContent = 'Please enter a valid URL.';
errorElement.classList.remove('hidden');
return;
}
try {
const response = await fetch(`https://api.shrtco.de/v2/shorten?url=${longUrl}`);
const data = await response.json();
if (data.ok) {
shortenedUrlElement.textContent = data.result.full_short_link;
copyButton.classList.remove('hidden');
copyButton.onclick = () => {
navigator.clipboard.writeText(data.result.full_short_link);
alert('Shortened URL copied to clipboard!');
};
} else {
throw new Error(data.error);
}
} catch (error) {
errorElement.textContent = 'Failed to shorten the URL. Please try again.';
errorElement.classList.remove('hidden');
}
});