jQuery

How to get URL parameters using jQuery

Using parameters in a URL is probably the easiest way to pass variables from one web page to another. In this tutorial, we are going to see how to get URL parameters using jQuery.
 

 

 

Method 1: Get URL parameters using jQuery
function getParameter(p)
{
    var url = window.location.search.substring(1);
    var varUrl = url.split('&');
    for (var i = 0; i < varUrl.length; i++)
    {
        var parameter = varUrl[i].split('=');
        if (parameter[0] == p)
        {
            return parameter[1];
        }
    }
}

Assuming the URL is: “stackhowto.com/t.html?name=alex-babtise&age=25&address=california”. Here is how we can retrieve the value of the “name” variable:

​var name = getParameter('name');
console.log(name);

Output:

alex-babtise

 

Method 2: Get URL parameters using jQuery
let searchParams = new URLSearchParams(window.location.search)
searchParams.has('name') 
let name = searchParams.get('name')
console.log(name);

Output:

alex-babtise
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *