1

I have the following problem:

I am using the following function to retrieve the GET Parameters of an URL.

$(document).ready(function() {    
function getParam(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for ( var i = 0; i < vars.length; i++) {
                var pair = vars[i].split("=");
                if (pair[0] == variable) {
                    return pair[1];
                }
            }
            return ("");
        }
});

For example I have the URL http://example.com/?radius=5&lat=31.312 and I want to have the value of "radius"

var radius = getParam("radius");

Problem: In the URL I can see that radius has the value "5" but when I run console.log("radius " + radius); only after refreshing the browser page the value is shown correctly.

Why isn't the value retrieved correctly while loading the page on the first time? Does anybody has an idea for fixing this?

I am getting the parameters by a submitted form from another html and I want to use them on the current html

2 Answers2

0

This is a standard program for reading query string parameters: http://www.webreference.com/programming/javascript/jkm/2.html

Upload param.js, from the link at the bottom of that page, to your site and include it in a script element:

 <script src="[...]param.js"></script> 

Regardless of what stage of loading the page is in you can call the function param() to read parameters, for instance:

var q = param();
var radius = q.radius;
Joseph Myers
  • 6,434
  • 27
  • 36
0

you van make function like this :

$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});

// Get object of URL parameters
var allVars = $.getUrlVars();

// Getting URL var by its nam
var byName = $.getUrlVar('name');
Mohamed AbdElRazek
  • 1,654
  • 14
  • 17