• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Page Details / Get all URL parameters from a query string

Get all URL parameters from a query string

Get all URL parameters from a query string

A technique you can use to get all query string parameters and push them into an object of key/value pairs.

/**
 * Get the URL parameters
 * source: https://css-tricks.com/snippets/javascript/get-url-variables/
 * @param  {String} url The URL
 * @return {Object}     The URL parameters
 */
var getParams = function (url) {
	var params = {};
	var parser = document.createElement('a');
	parser.href = url;
	var query = parser.search.substring(1);
	var vars = query.split('&');
	for (var i = 0; i < vars.length; i++) {
		var pair = vars[i].split('=');
		params[pair[0]] = decodeURIComponent(pair[1]);
	}
	return params;
};

Use it like this:

// Get parameters from the current URL
getParams(window.location.href);

// Get parameters from a URL string
var url = 'https://gomakethings.com?sandwhich=chicken%20salad&bread=wheat';
getParams(url);

Source

https://vanillajstoolkit.com/helpers/getparams/

https://css-tricks.com/snippets/javascript/get-url-variables/

Page Details

Related Snippets:

  • Get the height of the viewport
  • Get the Width and Height of the Window
  • Top Window
  • Delayed Image Loader

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers