html url shortener "#/"
- User
html url shortener "#/"
- Assistant
html url shortener "#/"
It sounds like you're interested in creating (or understanding) a simple HTML URL shortener that uses a hash fragment (i.e., URLs with #/) for routing or for representing short URLs.
Let me clarify a few things and then provide a basic example:
What is a URL shortener?
Usually, it's a service that takes a long URL and generates a shorter link which redirects to the original URL.
Use of #/ in URL
The hash fragment (everything after #) is never sent to the server; it’s only handled client-side, often used in single-page apps for routing without full page reloads.
Simple hash-based URL shortener
Since without a backend a URL shortener can't truly store mappings, a hash-based shortener can store mappings in the browser (localStorage) and use the hash to encode the short code.
#/This example lets a user enter a long URL, creates a short code, stores the mapping in localStorage, and then, when visiting a URL with #/code, redirects to the stored original URL.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Simple Hash URL Shortener</title>
<style>
body { font-family: Arial, sans-serif; max-width: 600px; margin: 2em auto; }
input[type="text"] { width: 80%; padding: 0.5em; font-size: 1em; }
button { padding: 0.5em 1em; font-size: 1em; }
#result { margin-top: 1em; }
</style>
</head>
<body>
<h1>Simple Hash URL Shortener (#/)</h1>
<p>Paste a long URL and get a short URL with <code>#/code</code> format.</p>
<input type="text" id="longUrl" placeholder="https://example.com/some/very/long/url" />
<button id="shortenBtn">Shorten URL</button>
<div id="result"></div>
<script>
// Helper to generate a random 5-char code
function generateCode(length = 5) {
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
let code = '';
for (let i = 0; i < length; i++) {
code += chars.charAt(Math.floor(Math.random() * chars.length));
}
return code;
}
// Save mapping in localStorage
function saveMapping(code, longUrl) {
const mappings = JSON.parse(localStorage.getItem('urlMappings') || '{}');
mappings[code] = longUrl;
localStorage.setItem('urlMappings', JSON.stringify(mappings));
}
// Get mapping from localStorage
function getMapping(code) {
const mappings = JSON.parse(localStorage.getItem('urlMappings') || '{}');
return mappings[code];
}
// Create short URL and display
document.getElementById('shortenBtn').addEventListener('click', () => {
let longUrl = document.getElementById('longUrl').value.trim();
if (!longUrl) {
alert('Please enter a URL.');
return;
}
// Normalize URL (add http if missing)
if (!/^https?:\/\//i.test(longUrl)) {
longUrl = 'http://' + longUrl;
}
const code = generateCode();
saveMapping(code, longUrl);
const shortUrl = location.origin + location.pathname + '#/' + code;
const resultDiv = document.getElementById('result');
resultDiv.innerHTML = `Short URL: <a href="${shortUrl}">${shortUrl}</a>`;
});
// On page load: Check if URL has #/code and redirect if mapping exists
window.addEventListener('load', () => {
const hash = location.hash;
if (hash.startsWith('#/')) {
const code = hash.slice(2);
const longUrl = getMapping(code);
if (longUrl) {
// Redirect
window.location.href = longUrl;
} else {
document.getElementById('result').innerText = 'Short URL not found.';
}
}
});
</script>
</body>
</html>
localStorage.#/code.http://yourdomain.com/#/abcde), the script extracts the code in the hash and finds the original URL.localStorage. Other users won’t be able to use the short URLs if opened elsewhere.If you want a full backend shortener with #/ style routing (e.g., SPA with React Router), that’s a different setup. But this covers how to make a very simple HTML + JS hash-based shortener.
Let me know if you want an example with server-side code or using frameworks!
Not yet included in a published release.