Java Scripts ·
JavaScript URL and String Snippets: Query, Slice, Events
By Manu Mathew, Technical Lead Engineer · Published 11 January 2014 · Last updated 5 October 2026

In PHP you read a URL parameter with $_GET['name'], and in Perl with $q->param('name'). In the browser, JavaScript has a built-in tool for it too. This page brings together several short notes on JavaScript URL parameters and strings I wrote in 2014. I have rewritten them in one place, with tested examples and current advice, so you don’t have to piece the answer together from fragments.
Quick answer
Use new URLSearchParams(window.location.search).get('name') to read a query string value; it decodes the value and returns null when the parameter is missing. window.location gives every part of the current URL. slice(-n) returns the last n characters, and dispatchEvent() runs an event’s handlers from code.
I tested every example with jsdom 24 under Node.js 20, which follows the same web standards as current browsers.
How do I read values from the query string?
Create a URLSearchParams from window.location.search and call get().
// On https://www.example.com/page?name1=value1&name2=value%202
const params = new URLSearchParams(window.location.search);
params.get('name1'); // "value1"
params.get('name2'); // "value 2" (already decoded)
params.get('missing'); // null
params.has('name1'); // true
// All values as a plain object
const all = Object.fromEntries(params); // { name1: "value1", name2: "value 2" }
The older version of this note used a regular expression over location.href. It didn’t decode values such as %20, and it also picked up text after the #. URLSearchParams handles both, and getAll() returns every value when a name repeats.
How do I get the current page URL?
window.location.href is the full URL; origin, pathname, search and hash give its parts.
// On https://www.example.com:8080/shop/page?item=7#reviews
window.location.href; // "https://www.example.com:8080/shop/page?item=7#reviews"
window.location.origin; // "https://www.example.com:8080"
window.location.pathname; // "/shop/page"
window.location.search; // "?item=7"
window.location.hash; // "#reviews"
To work with any other URL in the same way, use new URL('https://…'), which has the same properties plus searchParams.
How do I get the last characters of a string?
str.slice(-n) returns the last n characters, and str.at(-1) the last one.
const ref = 'order_KZD_10457';
ref.slice(-5); // "10457" (negative numbers count from the end)
ref.slice(-1); // "7"
ref.at(-1); // "7"
ref.split('_').pop(); // "10457" (the part after the last underscore)
The older note used substr(), which still works but is marked as deprecated, so slice() is the safer choice in new code.
How do I trigger a change or click event from code?
Create an event and send it with dispatchEvent(), or call click() for clicks.
// Run a field's change handlers from code, as if the user had changed it
const field = document.getElementById('country');
field.value = 'IN';
field.dispatchEvent(new Event('change', { bubbles: true }));
// Simulate a click
document.getElementById('save').click();
// With jQuery:
// $('#country').val('IN').trigger('change');
Changing value in code doesn’t fire change by itself, which is why the event is sent explicitly. Add bubbles: true so handlers attached higher up the page, such as delegated jQuery handlers, see it too.
Frequently asked questions
Does URLSearchParams work in all browsers?
Yes. Every current browser supports it; it was missing only in Internet Explorer, which Microsoft no longer supports. If you must support a very old browser, a small polyfill adds it.
How do I change a query string value without reloading the page?
Build a URL object from location.href, change url.searchParams with set() or delete(), then call history.replaceState(null, '', url), or pushState to add a history entry. The address bar updates and the page stays as it is.
How do I read a value from the URL hash?
location.hash gives the part after the # sign, including the # itself, so use location.hash.slice(1). If the hash holds key=value pairs, pass that string to new URLSearchParams() and read them with get().
Is it safe to put values from the URL into the page?
Only as text. Anyone can change a URL, so insert values with textContent rather than innerHTML, and validate them before using them in requests. Never put tokens or personal data in a query string, since URLs end up in logs and browser history.
Related notes: HTML forms in JavaScript; small PHP snippets.
Need help with your WordPress site?
Whether you need a new website or help with the one you have, I’m happy to talk it through. I’m based in Kozhikode and work with small businesses across Kerala and remotely; WordPress websites explains what I help with.
You can reach me through the contact section or email me at manu@manu.co.in. Tell me a little about your business and the site, and we’ll go from there.
An earlier version of the last-characters note reproduced a Stack Overflow answer (question 5873810, CC BY-SA). This version is rewritten in my own words, with a new example.
Sources: MDN: URLSearchParams; MDN: Location; MDN: String.prototype.slice(); MDN: String.prototype.substr() (deprecated); MDN: EventTarget.dispatchEvent(); Stack Overflow question 5873810.