This README was generated with the assistance of Claude AI (Anthropic)
A lightweight, easy-to-use Single Page Application (SPA) routing library for JavaScript with no dependencies.
- π¦ Lightweight: Minimal footprint with zero dependencies
- π Client-side Routing: Smooth SPA navigation with History API
- π Dynamic Routes: Support for parameterized routes like
/users/{id} - π² Nested Routes: Create hierarchical route structures
- π₯ Loading States: Built-in loading indicator support
- π« 404 Handling: Custom not-found route handler
- π§© Simple API: Intuitive method chaining
<script src="path/to/uroute.js"></script>// Initialize the router
const router = new URoute();
// Define basic routes
router.route('/', () => {
document.getElementById('app').innerHTML = '<h1>Home Page</h1>';
});
router.route('/about', () => {
document.getElementById('app').innerHTML = '<h1>About Us</h1>';
});
// Route with parameters
router.route('/profile/{id}', (params) => {
document.getElementById('app').innerHTML = `<h1>Profile ${params.id}</h1>`;
});
// Start the router
router.start();Define routes using the route() method:
router.route('/contact', () => {
document.getElementById('app').innerHTML = '<h1>Contact Page</h1>';
});Extract dynamic segments from URLs:
router.route('/blog/{category}/{postId}', (params) => {
const { category, postId } = params;
document.getElementById('app').innerHTML = `
<h1>${category} Post #${postId}</h1>
`;
});Create hierarchical routes with the child() method:
router.route('/admin', () => {
document.getElementById('app').innerHTML = `
<h1>Admin Dashboard</h1>
<div id="admin-content"></div>
`;
})
.child('/admin/users', () => {
document.getElementById('admin-content').innerHTML = '<h2>User Management</h2>';
})
.child('/admin/settings', () => {
document.getElementById('admin-content').innerHTML = '<h2>System Settings</h2>';
});Handle routes that don't match any defined patterns:
router.notFound(() => {
document.getElementById('app').innerHTML = `
<h1>404 - Page Not Found</h1>
<p>The page you're looking for doesn't exist.</p>
`;
});Manage loading indicators during route transitions:
router.whenLoading((isLoading) => {
const loader = document.getElementById('loader');
loader.style.display = isLoading ? 'block' : 'none';
});Navigate between routes using code:
document.getElementById('login-button').addEventListener('click', () => {
// Login logic here...
router.navigate('/dashboard');
});Creates a new router instance.
Defines a new route with the specified path and callback.
path(String): URL path patterncallback(Function): Handler function that receives route parametersoptions(Object, optional): Additional options for the route
Returns an object with child route methods for chaining.
Initializes the router and triggers the route matching for the current URL.
Returns the router instance for chaining.
Navigates to the specified path programmatically.
path(String): The path to navigate to
Returns the router instance for chaining.
Sets a handler function for routes that don't match any defined patterns (404).
callback(Function): Handler function for "not found" routes
Returns the router instance for chaining.
Sets a handler function for loading states during route transitions.
callback(Function): Handler function that receives a boolean indicating loading state
Returns the router instance for chaining.
URoute works by:
- Intercepting URL changes (clicks on links or browser navigation)
- Preventing default browser behavior that would request a new page
- Using the History API to update the URL without a page reload
- Matching the current URL against defined route patterns
- Extracting parameters from dynamic segments
- Executing the corresponding route callback
This provides a smooth, SPA-like navigation experience without requiring a full page reload.
URoute relies on the HTML5 History API, which is supported in all modern browsers:
- Chrome 5+
- Firefox 4+
- Safari 5+
- Opera 11.5+
- IE 10+
- Edge (all versions)
No License, Feel free to use