Get the values from the “GET” parameters of URL Using JavaScript

If you want to Use JavaScript by Default feature
Visit MDN :- https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams

Like we have URL http://www.example.com/t.html?a=1&b=3&c=m
and we want to get the value assigned to a i.e. 1 then w…


This content originally appeared on DEV Community and was authored by Shaswat Raj

If you want to Use JavaScript by Default feature
Visit MDN :- https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams

Like we have URL http://www.example.com/t.html?a=1&b=3&c=m
and we want to get the value assigned to a i.e. 1 then when we will call getParameterByName('a') that will return 1

function getParameterByName(name, url = window.location.href) {
    name = name.replace(/[\[\]]/g, '\\$&');
    var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, ' '));
}

Alternative Inbuilt JavaScript Function

var url_string = "http://www.example.com/t.html?a=1&b=3&c=m2-m3-m4-m5"; //window.location.href
var url = new URL(url_string);
var c = url.searchParams.get("c");
console.log(c);

or

function getparam(param,url){
if(!url) url = window.location.href;
let urlParams = new URL(url);
return urlParams.searchParams.get(param);
}

Minified and Fastest Version of the Function

function getparam(a,e){return e||(e=window.location.href),new URL(e).searchParams.get(a)}


This content originally appeared on DEV Community and was authored by Shaswat Raj


Print Share Comment Cite Upload Translate Updates
APA

Shaswat Raj | Sciencx (2022-01-14T13:19:45+00:00) Get the values from the “GET” parameters of URL Using JavaScript. Retrieved from https://www.scien.cx/2022/01/14/get-the-values-from-the-get-parameters-of-url-using-javascript/

MLA
" » Get the values from the “GET” parameters of URL Using JavaScript." Shaswat Raj | Sciencx - Friday January 14, 2022, https://www.scien.cx/2022/01/14/get-the-values-from-the-get-parameters-of-url-using-javascript/
HARVARD
Shaswat Raj | Sciencx Friday January 14, 2022 » Get the values from the “GET” parameters of URL Using JavaScript., viewed ,<https://www.scien.cx/2022/01/14/get-the-values-from-the-get-parameters-of-url-using-javascript/>
VANCOUVER
Shaswat Raj | Sciencx - » Get the values from the “GET” parameters of URL Using JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/14/get-the-values-from-the-get-parameters-of-url-using-javascript/
CHICAGO
" » Get the values from the “GET” parameters of URL Using JavaScript." Shaswat Raj | Sciencx - Accessed . https://www.scien.cx/2022/01/14/get-the-values-from-the-get-parameters-of-url-using-javascript/
IEEE
" » Get the values from the “GET” parameters of URL Using JavaScript." Shaswat Raj | Sciencx [Online]. Available: https://www.scien.cx/2022/01/14/get-the-values-from-the-get-parameters-of-url-using-javascript/. [Accessed: ]
rf:citation
» Get the values from the “GET” parameters of URL Using JavaScript | Shaswat Raj | Sciencx | https://www.scien.cx/2022/01/14/get-the-values-from-the-get-parameters-of-url-using-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.