Showing posts with label URL Parameters. Show all posts
Showing posts with label URL Parameters. Show all posts

Tuesday, September 18, 2018

JavaScript helper function - obtain parameters from URL string


The following is a JavaScript helper function - I've used to obtain the parameters from a URL string.
In this example - the parameter value I am looking for are those that appear after the name string

1) Call the script as such:
<script language="JavaScript">
            filepath=GetParam("filepath"); 
</script>


2) The GetParam function is as such:
<script language="JavaScript">
function GetParam(name)
{
 var start=location.search.indexOf("?"+name+"=");
  if (start<0) start=location.search.indexOf("&"+name+"=");
   if (start<0) return '';
   start += name.length+2;
   var end=location.search.indexOf("&",start)-1;
   if (end<0) end=location.search.length;
    var result=location.search.substring(start,end);
    var result='';
    for(var i=start;i<=end;i++)
    {
     var c=location.search.charAt(i);
     result=result+(c=='+'?' ':c);
    }
   return unescape(result);
}
</script>