Reputation: 293
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)
Upvotes: 1
Views: 818
Reputation: 1027
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)
Upvotes: 2