How can I get query string values in JavaScript

0 votes
Is there a plugin-less way of retrieving query string values via jQuery (or without)?

If so, how? If not, is there a plugin which can do so?
Jul 27, 2020 in Java-Script by kartik
• 37,520 points
1,447 views

1 answer to this question.

0 votes

Hello @kartik,

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, ' '));
}

Hope it helps!!

Thank you!!

answered Jul 27, 2020 by Niroj
• 82,840 points
Hello niroj
why you'd make this a jQuery plugin when it doesn't have any jQuery specific code?

Hello @Pooja,

 If you don't have a framework your functions will just get spread across your code. If you extend jQuery you will have a scope for your functions, it won't be floating around somewhere in your code.

Thank you... I have implemented what you have suggested and it worked... cheers!!

Related Questions In Java-Script

0 votes
1 answer

How can I replace every instance of a string in JavaScript?

In JavaScript, replacing all instances of a ...READ MORE

answered 4 days ago in Java-Script by Navya
31 views
0 votes
0 answers

How can I iterate through keys and values in JavaScript?

With the help of code, can you ...READ MORE

4 days ago in Java-Script by Ashutosh
• 12,620 points
18 views
0 votes
0 answers

How can I assign a variable to hold a multiline string in JavaScript?

With the help of code snippets and ...READ MORE

4 days ago in Java-Script by Ashutosh
• 12,620 points
21 views
0 votes
0 answers
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,193 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
3,051 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
3,009 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
45,094 views
0 votes
2 answers

How can I set a session var using javascript and get it via php code?

Yes it is possible. You can either ...READ MORE

answered Aug 9, 2020 in Java-Script by Okugbe
• 280 points
29,015 views
0 votes
2 answers

How can I set focus on an element in an HTML form using JavaScript?

Hi Kartik, try the following script <script>  (window.onload = ...READ MORE

answered Sep 24, 2020 in Java-Script by Okugbe
• 280 points
1,902 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP