Creating a basic URL shortener that works entirely on the client side (HTML + CSS + JS) and uses the URL hash (# or #/) to handle the shortened URLs is a fun project. Since we can't do real shortening (like storing mappings on a server or database), the best we can do is encode the original URL in some form in the hash part of the URL (like base64 or URI encoding) and then decode it on page load to perform a redirect.
Below is a simple example of a client-only URL shortener that:
- Takes a long URL input.
- Generates a shortened URL with the hash containing an encoded version of the original URL.
- When you visit the shortened URL, the page detects the hash, decodes the URL, and redirects the browser.
Features:
- Uses
#/ prefix in the URL hash.
- Encodes URLs via
encodeURIComponent + base64 (simple encoding).
- Decodes on load and redirects.
- Simple and minimal UI.
How to test:
- Open the page.
- Enter a long URL and click "Shorten".
- Get a URL that looks like:
yourdomain.com/#/encodedstring
- Paste that short URL in a new tab -> it redirects you to the original URL.
Complete HTML + CSS + JS:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Client-side URL Shortener</title>
<style>
body {
font-family: Arial, sans-serif;
height: 100vh;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
background: #f0f2f5;
}
.container {
background: white;
padding: 2rem;
border-radius: 8px;
box-shadow: 0 4px 10px rgba(0,0,0,0.1);
width: 400px;
text-align: center;
}
input[type="url"] {
width: 100%;
padding: 0.5rem;
font-size: 1rem;
margin-top: 1rem;
box-sizing: border-box;
}
button {
margin-top: 1rem;
padding: 0.5rem 1rem;
font-weight: bold;
background-color: #007bff;
border: none;
color: white;
border-radius: 4px;
cursor: pointer;
}
button:disabled {
background-color: #aaa;
cursor: not-allowed;
}
.output {
margin-top: 1rem;
padding: 0.5rem;
background: #e7f3ff;
border-radius: 4px;
word-break: break-all;
user-select: all;
}
.note {
font-size: 0.85rem;
color: #666;
margin-top: 0.5rem;
}
</style>
</head>
<body>
<div class="container">
<h1>URL Shortener</h1>
<p>Enter a long URL to get a shortened link (client-side only):</p>
<input id="urlInput" type="url" placeholder="https://example.com" required />
<button id="shortenBtn" disabled>Shorten URL</button>
<div class="output" id="output" style="display:none;"></div>
<div class="note">Copy the shortened URL and visit it in the same page to redirect.</div>
</div>
<script>
const urlInput = document.getElementById('urlInput');
const shortenBtn = document.getElementById('shortenBtn');
const output = document.getElementById('output');
// Enable the button only if input is a valid URL
urlInput.addEventListener('input', () => {
try {
new URL(urlInput.value);
shortenBtn.disabled = false;
} catch {
shortenBtn.disabled = true;
}
});
// Encode function: encode URL then base64 encode
function encodeUrl(url) {
return btoa(encodeURIComponent(url));
}
// Decode function: base64 decode, then decodeURIComponent
function decodeUrl(encoded) {
try {
return decodeURIComponent(atob(encoded));
} catch {
return null;
}
}
// Generate short URL
shortenBtn.addEventListener('click', () => {
const longUrl = urlInput.value.trim();
if (!longUrl) return;
// Basic validation
try {
new URL(longUrl);
} catch {
alert('Invalid URL');
return;
}
const encoded = encodeUrl(longUrl);
const shortUrl = `${location.origin}${location.pathname}#/${encoded}`;
output.style.display = 'block';
output.textContent = shortUrl;
// Optionally select the output text for easy copying
const range = document.createRange();
range.selectNodeContents(output);
const sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
});
// On load, check if hash starts with #/ and redirect
window.addEventListener('load', () => {
if (location.hash.startsWith('#/')) {
const encoded = location.hash.slice(2);
const url = decodeUrl(encoded);
if (url) {
// Redirect after a tiny delay to show something before redirecting
document.body.innerHTML = `<p>Redirecting to <a href="${url}">${url}</a>...</p>`;
setTimeout(() => {
window.location.href = url;
}, 1500);
return;
} else {
document.body.innerHTML = `<p>Invalid short URL. Please go back <a href="${location.origin}${location.pathname}">home</a>.</p>`;
return;
}
}
});
</script>
</body>
</html>
Explanation
- We encode the original URL using
encodeURIComponent to handle special chars, then btoa to base64 encode it. This makes the hash smaller and safer.
- The shortened link looks like:
https://yourdomain.com/#/encodedstring
- On page load, if the URL contains such a hash, we decode it and redirect.
- All logic happens client-side: no database, no backend, no server needed.
- Because it’s client-side only, it’s not a “proper” URL shortener — the URL is still fully included in the hash in encoded form — but it works for seed sharing or demos.
If you want me to help extend it to use a server or generate shorter hashes, or integrate with some server-less backend API, I can do that as well!