gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
html css js url shortener dataview "#/"
- User
html css js url shortener dataview "#/"
- Assistant
html css js url shortener dataview "#/"
Creating a basic URL shortener with an HTML UI, CSS styling, and JavaScript logic that uses the URL hash (#) for routing or state management can be a nice small project.
You mentioned dataview and "#/" — I interpret that as perhaps wanting a simple SPA-like interface where the visible area changes based on the URL hash (routing), and you want the shortener to work in that context.
I'll provide a simple example:
#/: The home page with a form to enter a URL to shorten.#/list: A page that shows the list of shortened URLs stored in localStorage (our "dataview").localStorage.#s/abc12 which redirect to the target URL.index.html#s/abc12 it does the redirect.<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Simple URL Shortener SPA</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 600px;
margin: 2rem auto;
padding: 0 1rem;
}
h1 {
text-align: center;
}
input[type="url"] {
width: 80%;
padding: 0.5rem;
}
button {
padding: 0.5rem 1rem;
margin-left: 0.5rem;
}
.short-url {
margin-top: 1rem;
font-weight: bold;
color: green;
}
nav {
margin-bottom: 1rem;
}
nav a {
margin-right: 1rem;
text-decoration: none;
color: blue;
}
nav a.active {
font-weight: bold;
}
ul {
list-style: none;
padding-left: 0;
}
li + li {
margin-top: 0.5rem;
}
.error {
color: red;
margin-top: 1rem;
}
</style>
</head>
<body>
<h1>Simple URL Shortener</h1>
<nav>
<a href="#/" id="nav-home">Home</a>
<a href="#/list" id="nav-list">Saved URLs</a>
</nav>
<div id="app">
<!-- Views will be inserted here -->
</div>
<script>
(() => {
// Storage key for saved URLs
const STORAGE_KEY = 'shortener_urls';
// Simple function to generate 5-char random alphanumeric ID
function generateId() {
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
let id = '';
for(let i=0; i<5; i++) {
id += chars.charAt(Math.floor(Math.random() * chars.length));
}
return id;
}
// Load URLs object from localStorage
function loadUrls() {
const data = localStorage.getItem(STORAGE_KEY);
return data ? JSON.parse(data) : {};
}
// Save URLs object to localStorage
function saveUrls(urls) {
localStorage.setItem(STORAGE_KEY, JSON.stringify(urls));
}
// Shorten a URL and save
function shortenUrl(url) {
const urls = loadUrls();
// Check if already shortened
for (const key in urls) {
if (urls[key] === url) return key;
}
let id;
do {
id = generateId();
} while(urls[id]);
urls[id] = url;
saveUrls(urls);
return id;
}
// Render Views
const app = document.getElementById('app');
const navHome = document.getElementById('nav-home');
const navList = document.getElementById('nav-list');
function renderHome() {
navHome.classList.add('active');
navList.classList.remove('active');
app.innerHTML = `
<form id="shorten-form">
<input type="url" id="input-url" placeholder="Enter URL to shorten" required />
<button type="submit">Shorten</button>
</form>
<div id="result"></div>
<div id="error" class="error"></div>
`;
const form = document.getElementById('shorten-form');
const input = document.getElementById('input-url');
const result = document.getElementById('result');
const error = document.getElementById('error');
form.addEventListener('submit', e => {
e.preventDefault();
error.textContent = '';
result.textContent = '';
let url = input.value.trim();
// Add https:// if missing for easier usage
if (!/^https?:\/\//i.test(url)) {
url = 'http://' + url;
}
try {
new URL(url); // Throws if invalid
} catch {
error.textContent = 'Invalid URL.';
return;
}
const id = shortenUrl(url);
const shortUrl = `${window.location.origin}${window.location.pathname}#s/${id}`;
result.innerHTML = `Short URL: <a href="${shortUrl}" target="_blank">${shortUrl}</a>`;
input.value = '';
});
}
function renderList() {
navHome.classList.remove('active');
navList.classList.add('active');
const urls = loadUrls();
app.innerHTML = `
<h2>Saved URLs</h2>
<ul id="url-list">
${Object.keys(urls).length === 0 ? '<li>No URLs saved yet.</li>' : ''}
</ul>
`;
if (Object.keys(urls).length > 0) {
const ul = document.getElementById('url-list');
for (const id in urls) {
const shortUrl = `${window.location.origin}${window.location.pathname}#s/${id}`;
const li = document.createElement('li');
li.innerHTML = `<a href="${shortUrl}" target="_blank">${shortUrl}</a> → <a href="${urls[id]}" target="_blank">${urls[id]}</a>`;
ul.appendChild(li);
}
}
}
// Router
function router() {
const hash = location.hash || '#/';
// If short URL redirect
if (hash.startsWith('#s/')) {
const id = hash.slice(3);
const urls = loadUrls();
if (urls[id]) {
// Redirect to original url
window.location.href = urls[id];
return;
} else {
app.innerHTML = '<p>Short URL not found.</p>';
navHome.classList.remove('active');
navList.classList.remove('active');
return;
}
}
// Normal views
if (hash === '#/' || hash === '') {
renderHome();
} else if (hash === '#/list') {
renderList();
} else {
app.innerHTML = '<p>Page not found.</p>';
navHome.classList.remove('active');
navList.classList.remove('active');
}
}
window.addEventListener('hashchange', router);
window.addEventListener('load', router);
})();
</script>
</body>
</html>
#/), enter a URL (e.g. https://example.com) and press "Shorten".http://yourdomain/index.html#s/abcde.#/list to see all shortened URLs stored locally.localStorage. If you refresh or open in another browser, data is lost.location.hash and listen to hashchange to implement simple routing.#s/id for redirect entries.URL object.If you want a more advanced version, or something with server interaction, please tell me!
Not yet included in a published release.