Saturday, June 18, 2016

Get url parameter through jquery

Sometimes we need the parameter from url through jquery.

Url:- http://test.html?id=4

<html>
<head>
<title>test</title>
</head>
<body>

<script>
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 tech = getUrlParameter('id');
alert(tech);
</script>


</body>
<html>

No comments:

Post a Comment