jQuery - Get URL Parameters

0 votes
209 views
added Jul 4, 2018 in jQuery by anonymous
//Example website URL
 "http://yoursite.com/?language=php&blog=techtalk". 

//Create function which returns value of any parameters variable
function GetURLParameter(sParam) {
    var sPageURL = window.location.search.substring(1),
        sURLVariables = sPageURL.split('&');

    for (var i = 0; i < sURLVariables.length; i++) {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam)   {
          return sParameterName[1];
        }
    }
}​

//variable
var lang = GetURLParameter('php');
var blog = GetURLParameter('techtalk');

//Above variable will output:
// --> 'lang' variable = 'php' value
// --> 'blog' variable =  'techtalk' value
lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...