New to jQuery and I'm having trouble getting the parameters of a url that my server generates. I have a url like this:
<span class="popuptest"><a href="www.example.com/test?param1=1¶m2=2">find param</a></span>
my jquery function looks like so:
$(function() {
$('.popuptest a').live('click', function(event) {
$.extend({
getUrlVars: function(){
var vars = [], hash;
var hashes = this.href.slice(this.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];
}
});
var second = getUrlVars()["param2"];
alert(second);
return false;
});
});
clicking on the link should show me "2", however I get nothing... any help for a jQuery noob? thanks in advance!
I found this on a blog: http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html
Your jquery could be
things to note:
small improvement, parse the url only once, return array or params:
http://jsfiddle.net/shakhal/gXM3u/
So simple you can use any url and get value in Javascript.And this is Complete set of codes
from here
You don't need jQuery for that purpose you can use the pure JavaScript:
and you can call the function in this way..
getParameterByName(param1,href of your link)
DEMO