Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get "GET" request parameters in JavaScript? [duplicate]

Tags:

javascript

How to get "GET" variables from request in JavaScript?

Does jQuery or YUI! have this feature built-in?

like image 269
Daniel Silveira Avatar asked May 06 '09 18:05

Daniel Silveira


2 Answers

Update June 2021:

Today's browsers have built-in APIs for working with URLs (URL) and query strings (URLSearchParams) and these should be preferred, unless you need to support some old browsers or Opera mini (Browser support).

Original:

All data is available under

window.location.search 

you have to parse the string, eg.

function get(name){    if(name=(new RegExp('[?&]'+encodeURIComponent(name)+'=([^&]*)')).exec(location.search))       return decodeURIComponent(name[1]); } 

just call the function with GET variable name as parameter, eg.

get('foo'); 

this function will return the variables value or undefined if variable has no value or doesn't exist

like image 123
Rafael Avatar answered Sep 21 '22 17:09

Rafael


You could use jquery.url I did like this:

var xyz = jQuery.url.param("param_in_url"); 

Check the source code

Updated Source: https://github.com/allmarkedup/jQuery-URL-Parser

like image 25
Kaos Avatar answered Sep 18 '22 17:09

Kaos