Course contentsShow
← Web Development
Lesson 959 of 2,98217. URL and History ManagementFree lesson

Common URLSearchParams Patterns

Practical examples like search filters, pagination state, and form data encoding.

Common URLSearchParams Patterns

What you'll learn: How to use URLSearchParams in real-world scenarios like search filters, pagination, and form data encoding.

Now that you understand the mechanics of URLSearchParams, let's explore how developers actually use it in practice. These patterns appear constantly in modern web applications.

Search Filters Pattern

When users filter search results, you want their selections reflected in the URL so they can bookmark or share the filtered view:

// User selects category and price range
const params = new URLSearchParams(window.location.search);
params.set('category', 'electronics');
params.set('minPrice', '50');
params.set('maxPrice', '200');

// Update URL without reload
window.location.search = params.toString();
// Result: ?category=electronics&minPrice=50&maxPrice=200

Pagination State

Keep track of which page a user is viewing:

const params = new URLSearchParams(window.location.search);
const currentPage = params.get('page') || '1';
const itemsPerPage = params.get('limit') || '20';

// Navigate to next page
params.set('page', String(Number(currentPage) + 1));
window.location.search = params.toString();

Form Data Encoding

Convert form data for GET requests:

const formData = new FormData(document.querySelector('form'));
const params = new URLSearchParams(formData);

// Creates proper query string from form
const searchURL = `/search?${params.toString()}`;
// Example: /search?query=javascript&sort=newest&published=true

These patterns make your app's state shareable and navigable through URLs—users can bookmark specific views or use the back button intuitively.

Key Takeaway: URLSearchParams shines in search filters, pagination, and form encoding, turning complex state into clean, shareable URLs.