Creating a simple URL shortener using HTML, CSS, and JavaScript can be done in various ways. Since you mentioned SharedStorage and "#/" in the context, I'll assume you want a client-side app that keeps track of URL mappings in browser Shared Storage (a Web API related to ad tech and privacy sandbox, but not widely available yet). However, SharedStorage is experimental and not available in all browsers, so for this answer I'll interpret that you might want a solution that uses:
Client-side storage (e.g. localStorage or sessionStorage) to keep a map of short URLs to original URLs.
A URL structure like /#/shortCode, where shortCode maps to the original URL.
Simple HTML/CSS/JS that allows adding long URLs and getting short URLs.
What is SharedStorage?
SharedStorage API is part of the Privacy Sandbox for web browsers and currently supported mainly in Chromium-based browsers with flags enabled. It's different from common storage APIs like localStorage or IndexedDB. It's mostly used for privacy-preserving ad tech at the moment.
If you want to use a stable API, it's better to fallback to localStorage or IndexedDB.
Implementing a Simple Client-side URL Shortener with /#/ routing
Below is an example of:
Letting user input a long URL
Generating a short code (random 4 or 5 characters)
Storing the map in localStorage
When page loads with /#/shortCode, redirect the browser to the real URL.
Full Example
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Simple URL Shortener</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 2rem;
}
input, button {
font-size: 1rem;
padding: 0.5rem;
width: 100%;
margin: 0.5rem 0;
}
.short-link {
margin: 1rem 0;
font-weight: bold;
}
.error {
color: red;
}
</style>
</head>
<body>
<h1>Simple URL Shortener</h1>
<input type="url" id="urlInput" placeholder="Enter a URL to shorten" />
<button id="shortenBtn">Shorten URL</button>
<div class="short-link" id="result"></div>
<div class="error" id="error"></div>
<script>
const storageKey = 'urlShortenerMap';
// Load stored map or create empty
function getUrlMap() {
try {
return JSON.parse(localStorage.getItem(storageKey)) || {};
} catch {
return {};
}
}
function saveUrlMap(map) {
localStorage.setItem(storageKey, JSON.stringify(map));
}
// Generate random short code with 5 chars (letters and digits)
function generateShortCode(length = 5) {
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
let result = '';
for(let i=0; i<length; i++) {
result += chars.charAt(Math.floor(Math.random()*chars.length));
}
return result;
}
// Validate URL format (simple)
function isValidUrl(string) {
try {
new URL(string);
return true;
} catch {
return false;
}
}
function shortenUrl(longUrl) {
const map = getUrlMap();
// Check if URL already exists (avoid duplicates)
for (const [key, val] of Object.entries(map)) {
if(val === longUrl) {
return key;
}
}
// Else generate new short code and make sure it's unique
let shortCode;
do {
shortCode = generateShortCode();
} while (map[shortCode]);
map[shortCode] = longUrl;
saveUrlMap(map);
return shortCode;
}
function showResult(shortCode) {
const url = `${location.origin}/#/${shortCode}`;
document.getElementById('result').textContent = `Short URL: ${url}`;
}
function showError(msg) {
document.getElementById('error').textContent = msg;
}
document.getElementById('shortenBtn').addEventListener('click', () => {
document.getElementById('error').textContent = '';
document.getElementById('result').textContent = '';
const urlInput = document.getElementById('urlInput').value.trim();
if (!urlInput) {
showError('Please enter a URL.');
return;
}
if(!isValidUrl(urlInput)) {
showError('Invalid URL format.');
return;
}
const shortCode = shortenUrl(urlInput);
showResult(shortCode);
});
// On page load: check if the URL has a #/shortCode and redirect
window.addEventListener('load', () => {
const hash = location.hash;
if(hash.startsWith('#/')) {
const shortCode = hash.slice(2);
const map = getUrlMap();
if(map[shortCode]) {
window.location.href = map[shortCode];
} else {
document.getElementById('error').textContent = 'Short URL not found.';
}
}
});
</script>
</body>
</html>
How it works:
User inputs a long URL, clicks Shorten URL
The app:
Validates the URL
Generates or retrieves a short code
Stores mapping in localStorage
Shows you a short URL like http://localhost/#/abc12
When anyone opens the page with /#/abc12, the script runs on page load and:
Looks up abc12 in storage
Redirects browser to destination URL, or shows error if not found
Notes
This is a purely client-side demo, so the short URLs only work in the same browser & device (localStorage scoped).
In practice, URL shorteners require backend and public DNS/domain to be truly shareable.
SharedStorage API is experimental; if you want it used you can clarify, but current best practice for client storage is localStorage or IndexedDB for such simple apps.
If you want me to help you build a version using SharedStorage API or with a backend - let me know!