jQuery Equivalent for isset($_GET['var'])

jquery

Solution

You would have to try something similar to this: (you don't have to use the variables, but you can if you want)

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

if ($.urlParam('variable_name') != '') {  // variable_name would be the name of your variable within your url following the ? symbol
    //execute if empty
} else {
    // execute if there is a variable
}

If you would like to use the variables:

// example.com?param1=name&param2=&id=6
$.urlParam('param1'); // name
$.urlParam('id');        // 6
$.urlParam('param2');   // null

Problem

I currently have a jQuery function that I need to know if there is any `GET` data present. I do not need the data from the querystring, just whether there is any or not to run one of two functions. Equivalent PHP: ``` if (isset($_GET['datastring'])) { // Run this code } else { // Else run this code } ```

Original source

Related problems