Pages

Wednesday, 18 December 2024

URL Shortener Tool



URL Shortener

URL Shortener Tool

* { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: 'Poppins', sans-serif; background-color: #f7f7f7; display: flex; justify-content: center; align-items: center; height: 100vh; color: #333; } .container { text-align: center; background-color: #ffffff; border-radius: 10px; padding: 30px; box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1); width: 100%; max-width: 500px; } h1 { font-size: 32px; color: #2c3e50; margin-bottom: 20px; } .input-container { display: flex; justify-content: center; gap: 10px; } #url-input { width: 70%; padding: 10px; border: 2px solid #3498db; border-radius: 5px; font-size: 16px; } button { padding: 10px 20px; background-color: #3498db; color: white; border: none; border-radius: 5px; cursor: pointer; font-size: 16px; } button:hover { background-color: #2980b9; } .output-container { margin-top: 20px; } #short-url { width: 100%; padding: 10px; margin-top: 10px; border: 2px solid #2ecc71; border-radius: 5px; font-size: 16px; color: #333; } #short-url:focus { outline: none; } button:focus { outline: none; } button:active { transform: scale(0.98); } function shortenURL() { const urlInput = document.getElementById("url-input").value; if (urlInput === "") { alert("Please enter a URL."); return; } const shortUrl = generateShortUrl(urlInput); // Display the shortened URL document.getElementById("short-url").value = shortUrl; document.querySelector(".output-container").style.display = "block"; } function generateShortUrl(url) { const baseUrl = "https://short.ly/"; const randomPart = Math.random().toString(36).substring(2, 8); // Random string generator return baseUrl + randomPart; } function copyToClipboard() { const shortUrl = document.getElementById("short-url"); // Select the text field shortUrl.select(); shortUrl.setSelectionRange(0, 99999); // For mobile devices // Copy the text to the clipboard navigator.clipboard.writeText(shortUrl.value) .then(() => alert("Shortened URL copied to clipboard!")) .catch(err => console.error("Failed to copy text: ", err)); }