Ads Top

Query string example in javascript

How to get all URL parameters with the query string?

Query string URL parameters need when we jump current page to another page then we use Or we need query string when the required value from the current URL. The very important thing is the when getting URL value then decodes a URI component after then use param name as given below example. Query string parameters and value we get in an easy way with this demo.

C:/Users/ck/Desktop/queryString.html?paramName1=sheo&paramName2=Sagar

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Query String in javascript</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
    <form>
        <input type="text" name="Name" id="Name">
        <input type="text" name="phone" id="phone">
        <input type="hidden" value="" id="yourId1">
        <input type="hidden" value="" id="yourId2">
    </form>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script>
        jQuery(function () {
            var getUrlParameter = function getUrlParameter(sParam) {
                var sPageURL = decodeURIComponent(window.location.search.substring(1)),
                    sURLVariables = sPageURL.split('&'),
                    sParameterName,
                    i;
                for (i = 0; i < sURLVariables.length; i++) {
                    sParameterName = sURLVariables[i].split('=');

                    if (sParameterName[0] === sParam) {
                        return sParameterName[1] === undefined ? true : sParameterName[1];
                    }
                }
            };
            var param1Value = getUrlParameter("paramName1");
            var param2Value = getUrlParameter("paramName2");
            jQuery('#yourId1').val(param1Value);
            jQuery('#yourId2').val(param2Value);
        });
    </script>
</body>

</html>


No comments:

Copyright Reserved to Anything Learn. Powered by Blogger.