0

i am trying to get the querystring value in jquery and pass it to php page using ajax. but i am getting the full string like task_id=2 instead i want only value 2. how can i do this?

$('#stop').click(function(){
var time = $('#counter').text();
var url = location.search.substr(1)
    $.ajax({
                type: "POST",
                url: "ajax-calls/updatetimestamp.php",
                data: {time,url}
                success: function() {
                    location.reload();
                },
            });
})
CJAY
  • 6,989
  • 18
  • 64
  • 106

1 Answers1

0

You don't need jQuery for that purpose. You can use just some pure JavaScript:

function getParameterByName(name, url) {
    if (!url) 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, " "));
}

Usage:

// query string: ?foo=lorem&bar=&baz
var foo = getParameterByName('foo'); // "lorem"
var bar = getParameterByName('bar'); // "" (present with empty value)
var baz = getParameterByName('baz'); // "" (present with no value)
var qux = getParameterByName('qux'); // null (absent)
shubham715
  • 3,324
  • 1
  • 17
  • 27