Reputation: 1472
i am trying to get value from a location href link and was able to get the location href link through jquery and i used
var loc =$(location).attr('href');
using this i got the value
localhost/example.com/buy.php?ev=product_search&type=sb&search=sony&cat=3
now hot to get value of type , search and cat from this link ?
for eg
type=sb
search=sony
cat=3
my full code
$("input:radio[name=type_radio]").click(function() {
var loc =$(location).attr('href');
var value = $(this).val();
var type="condition";
alert(loc);
var dataString="value="+ value + "&type=" + type;
$('.srchcntntagn').css('display','block');
$('.by-product-list').addClass('opfxsc');
$.ajax({
type: "POST",
url: "modules/buy&sell/load_search.php",
data: dataString,
cache: false,
success: function(html){
$('.by-product-list').removeClass('opfxsc');
$('.by-product-list').html(html).fadeIn('slow');
$('.srchcntntagn').css('display','none');
}
});
});
Upvotes: 0
Views: 1391
Reputation: 742
I usually use this "gup" funciton found here: http://www.netlobo.com/url_query_string_javascript.html
You just add the function to your script then do something like:
var type = gup( 'type' );
This also eliminates the need for grabbing the URL with the .attr('href') call.
The function itself is:
function gup( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
Upvotes: 3
Reputation: 16010
location.search
will provide you with the query string of a url (including the ?).
You can strip the ? and split the string into arrays on &
var queries = location.search.substr(1).split('&');
If you are working with a string that isn't the current URL, try:
var queries = some_url_string.split('?')[1].split('&');
Either will give you an array like so:
['type=sb','search=sony','cat=3']
You can then split each element again on =
to get key/value pairs:
for(i=0;i<queries.length;i++) {
queries[i] = queries[i].split('=');
}
Which will give you:
[['type','sb'],['search','sony'],['cat','3']]
Alternatively, create a query object:
var queryObj = {};
for(i=0;i<queries.length;i++) {
var q = queries[i].split('=');
queryObj[q[0]] = q[1];
}
This will give you an object like so:
{
type: 'sb',
search: 'sony',
cat: '3'
}
However, if you really need individual variables, you could do:
for(i=0;i<queries.length;i++) {
var q = queries[i].split('=');
window[q[0]] = q[1];
}
This will create variables type
, search
, and cat
in the global scope. However, I do not recommend this as you may be overwriting existing variables depending on the querystring you're parsing.
Upvotes: 1
Reputation: 47956
Quick and simple -
&
.var url="localhost/example.com/buy.php?ev=product_search&type=sb&search=sony&cat=3";
var queryString = url.split('?')[1];
var variables = queryString.split('&');
var varObj = {};
$.each(variable,function(index,elem){
var valueKeyPair = elem.split('=');
varObj[valueKeyPair[0]] = valueKeyPair[1];
});
varObj
now contains all the info you need.
Object
cat: "3"
ev: "product_search"
search: "sony"
type: "sb"
To access the cat
value for example, all you have to do is use varObj['cat']
.
Upvotes: 1