1

I'm using this function (from accepted answer)

to get parameters from the url:

function getUrlParameter(sParam)
    {
        var sPageURL = window.location.search.substring(1);
        var sURLVariables = sPageURL.split('&');
        for (var i = 0; i < sURLVariables.length; i++) 
        {
            var sParameterName = sURLVariables[i].split('=');
            if (sParameterName[0] == sParam) 
            {
                return sParameterName[1];
            }
        }
    } 

Everything is working fine, but when I console.log a variable, I get the value but also the url is attached.

So for instance for address: http://dummy.com/?technology=jquery&blog=jquerybyexample I do:

var tech = getUrlParameter('technology'); and in console log I get:

jquery     ?technology=jquery&blog=jquerybyexample 

Through jQuery I'm setting the value of the field and the problem is that the whole string is attached. How to make this right? (I want only 'jquery' to be attached and appear in console log)

Community
  • 1
  • 1
Shepherd
  • 273
  • 1
  • 2
  • 12

1 Answers1

2

Try that

function getUrlVars()
    {
        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[hash[0]] = hash[1];
        }
        return vars;
    }

then

urlVars = getUrlVars()

in urlVars you find a key/value hash, print that out ;)

console.log(urlVars)
aqquadro
  • 1,027
  • 10
  • 17
  • Hi, thanks. I tried some similar scripts, but I still get the same result. Maybe there's something wrong in my application or sth :( – Shepherd Mar 21 '15 at 18:58
  • O.K. there is something wrong with my further scripting, your solution can be the answer to my question so I can accept is. Thanks for the support – Shepherd Mar 21 '15 at 19:02
  • Why `window.location.href.slice(window.location.href.indexOf('?') + 1)` is better than `location.search.substring(1)` ? – tutankhamun Mar 21 '15 at 19:51
  • same result ;) I paste the first rapidly idea. location.search.substring(1) is better :) – aqquadro Mar 21 '15 at 19:55