9

i saw some similar post at Stack, but i want to ask for specific function in jquery that is

$.urlParam('GETparameter')

Why simple if($.urlParam('GETparameter') === undefined) not working? And why there are complex answers at other questions?

Thank you for answers

4 Answers 4

18

Another solution here is the URLSearchParams function.

As an example, if your URL was https://example.com?foo=bar&baz=qux you could retrieve all of the parameters and then query the specific one like this:

var urlParams = new URLSearchParams(window.location.search); //get all parameters
var foo = urlParams.get('foo'); //extract the foo parameter - this will return NULL if foo isn't a parameter

if(foo) { //check if foo parameter is set to anything
    alert('FOO EXISTS');
}

if(foo == 'bar') { //check if foo parameter is 'bar'
    alert('FOO == BAR');
}
Sign up to request clarification or add additional context in comments.

3 Comments

This is clean solution but unfortunately IE doesn't support URLSearchParams.
@PinakiMukherjee yeah - depending on what you are coding for, most new small websites are ignoring Internet Explorer as a use case. Though it's important to note that IE is still supported by MS until Aug2021, the current usage estimate for IE is between 0.3% and 1.89%, which is an incredibly small percentage of people, who will honestly be dealing with a ton of issues from other websites in any case.
I agree, IE is like new IE6. Everyone will be happy on her demise :).
11

Update 2021

This is an overdue update, but as Frits's solution notes, using URLSearchParams is now the best approach.

const getQueryParameter = (param) => new URLSearchParams(document.location.search.substring(1)).get(param);

Answer from 2016

First off, $.urlParam is not an inherent jQuery function as far as I can tell, meaning you'll need to define it if you plan on using it.

When looking it up, I found a user-created function of the same name; I am going to assume that this is what you're referring to:

$.urlParam = function(name){
    var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
    return results[1] || 0;
}

It looks like this will return 0 if the parameter is not found, rather than undefined. If this function is in fact the one you're referencing, you'll want to do this instead:

if($.urlParam('GETparameter') === 0)

2 Comments

I also had to check for null before the return. $.urlParam = function(name){ var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href); if (results === null) { return 0; } return results[1]; }
Yay for URLSearchParams! :) :) stoked there's finally a better solution available for us to use.
1

If you want to check type of variable, you need to use something like:

if (typeof($.urlParam('GETparameter')) === 'undefined') {
  ..
}

typeof documentation

3 Comments

Why is this better than just checking === undefined? See 2ality.com/2013/04/check-undefined.html
In my opinion it's good practise, because if you don't have defined variable at all, you will get error: "ReferenceError: x is not defined". Less problems with further debug. See this post
You right, but i always use that syntax to avoid problems and simplify debug process.
0

You could try this:

var url_param       = $(location).attr('href').split("?");
var param           = (url_param[1]);

Where url_param[1] is your second element in array (in this case your url parameters, if existing).

Then you can check with if(param!='') { // do something }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.