1

If I have a url https://stackoverflow.com/?variable=12345 how can I check if there is a GET parameter in the URL and what it is equals to in JS or jQuery?

For example in PHP:

if(isset($_GET['variable']))
   $variable = $_GET['variable'];

Thanks.

2

1 Answer 1

1
function get_var(var_name){
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == var_name){return pair[1];}
       }
       return(false);
}

And to use:

var get_variable = get_var("variable");
if (get_variable !== '') {

  // Variable exists

}
Sign up to request clarification or add additional context in comments.

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.