Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»JavaScript»How to Get Query String Values using JavaScript

    How to Get Query String Values using JavaScript

    RahulBy RahulNovember 28, 20141 Min Read

    We can easily get values passed in url query string using JavaScript or jQuery. Here is an simple examples of getting query string values with JavaScript. To do this create a function like getParamValuesByName and add one of example codes given below.

    In the below examples function getParamValuesByName() will parse the query string values and returned matching values based on there named passed as parameter.

    Example 1:

    <script type="text/javascript">
    function getParamValuesByName (querystring) {
      var qstring = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
      for (var i = 0; i < qstring.length; i++) {
        var urlparam = qstring[i].split('=');
        if (urlparam[0] == querystring) {
           return urlparam[1];
        }
      }
    }
    var uid = getParamValuesByName('uid');
    var uname = getParamValuesByName('uname');
    </script>
    

    Example 2:

    <script type="text/javascript">
    function getParamValuesByName (querystring) {
        name = querystring.replace(/[[]/, "\[").replace(/[]]/, "\]");
        var regexS = "[\?&]" + name + "=([^&#]*)";
        var regex = new RegExp(regexS);
        var results = regex.exec(window.location.search);
        if (results == null) {
    	    return "";
    	}
        else {
    	    return decodeURIComponent(results[1].replace(/+/g, " "));
    	}
    }
    
    var uid = getParamValuesByName('uid');
    var uname = getParamValuesByName('uname');
    </script>
    
    get javascript querystring
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Redirect User to Another Webpage with JavaScript
    Next Article How to Kill Process by Name in Linux

    Related Posts

    How to Replace String in JavaScript

    Updated:June 13, 20222 Mins Read

    JavaScript Program to Add Two Numbers

    Updated:June 9, 20222 Mins Read

    How to Install ReactJS on Ubuntu 20.04

    3 Mins Read

    Remove First Character from String in JavaScript

    1 Min Read

    10 Useful JavaScript Console Methods

    Updated:June 9, 20223 Mins Read

    JavaScript Program to Remove Duplicate Array Elements

    Updated:July 20, 20211 Min Read

    1 Comment

    1. Dan on January 25, 2016 6:37 pm

      This was very helpful. Thank you!

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • What is CPU? – Definition, Types and Parts
    • What is the /etc/aliases file
    • What is the /etc/nsswitch.conf file in Linux
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.