Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to get query string values in JavaScript?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 126
    Comment on it

    A query string is an optional part of a URL that goes after the file name and begins with a question mark (?). For example, the following URL has a query string ?quryEg after the HTML file name: http://www.findnerd.com/file.html?quryEg

    Here is an example code to get the query string values:-

    <script type="text/javascript">
    function getquryvalues (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 userid = getquryvalues('userid');
    var username = getquryvalues('username');
    </script>
    

    In the above code regex.exec test for a match in the string and decodeURIComponent is used to decode the URI component.

    This is how you can use this code to get the query string value using javascript.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: