This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I get query string values?
I have the following querystring:
"active_tab=delivered&active_tab=all&active_tab=delivered&active_tab=outstanding
&active_tab=delivered&active_tab=outstanding&active_tab=all&active_tab=delivered&active_tab=outstanding&title_filter=conformance&title_filter=delivering&title_filter=packaging
&title_filter=delivering&title_filter=all&title_filter=delivering&title_filter=all&title_filter=packaging&title_filter=conformance&title_filter=packaging
&title_filter=delivering&title_filter=packaging&title_filter=ordered"
How would I parse the final title_filter ("ordered") and active_tab ("delivered") from the above querystring? Also, if that querystring variable doesn't exist, have it = ""
var query = {};
var largeString = "active_tab=delivered&active_tab=all&active_tab=delivered&active_tab=outstanding&active_tab=delivered&active_tab=outstanding&active_tab=all&active_tab=delivered&active_tab=outstanding&title_filter=conformance&title_filter=delivering&title_filter=packaging&title_filter=delivering&title_filter=all&title_filter=delivering&title_filter=all&title_filter=packaging&title_filter=conformance&title_filter=packaging&title_filter=delivering&title_filter=packaging&title_filter=ordered";
largeString.split('&').forEach(function(keyValue){
var kvp = p.split('=');
query[kvp[0]]= kvp[1];
});
if you need to support arrays:
largeString.split('&').forEach(function(keyValue){
var kvp = keyValue.split('=');
if(kvp[0] in query){
if(typeof(query[kvp[0]] === 'string')){
query[kvp[0]] = [query[kvp[0]]];
}
query[kvp[0]].push(kvp[1]);
}else{
query[kvp[0]] = kvp[1];
}
});
I modified the querystring to remove duplicates and then I did:
var active_tab = window.location.search.split('active_tab=')[1].split('&')[0]
var title_filter = window.location.search.split('title_filter=')[1].split('&')[0]
Related
This question already has answers here:
Set cookie wih JS, read with PHP problem
(5 answers)
Closed 7 years ago.
function CheckIfChecked(){
var strChoices = "";
var chk = document.getElementsByName("check_list[]");
var chklength = chk.length;
var e = document.getElementById("select_bulk");
var strUser = e.options[e.selectedIndex].value;
var buttn = document.getElementById("bulk");
Cookies.set(chk, e, strUser, butttn);}
// want to retrive this cookie in php code below
if(isset($_COOKIE['buttn']))
{ // do something
}
Try this,
In javascript
var key = "aaa"
var val = "bbb"
document.cookie = key + "=" + val;
In php
print_R($_COOKIE);
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 7 years ago.
I have an url like below
http://www.grcparfum.it/home.php?section=letteradelpresidente&lang=eng
in this URL language is english
lang=eng
i wanna call different JS file when lang is different
Here's a function that can get the querystring variables in JS for you:
function $_GET(q){
var $_GET = {};
if(document.location.toString().indexOf('?') !== -1){
var query = document.location
.toString()
.replace(/^.*?\?/, '')//Get the query string
.replace(/#.*$/, '')//and remove any existing hash string
.split('&');
for(var i=0, l=query.length; i<l; i++){
var aux = decodeURIComponent(query[i]).split('=');
$_GET[aux[0]] = aux[1];
}
}
return $_GET[q];
}
Does this help at all?
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Use the get paramater of the url in javascript
How can I get query string values in JavaScript?
In Javascript, how can I get the parameters of a URL string (not the current URL)?
like:
www.domain.com/?v=123&p=hello
Can I get "v" and "p" in a JSON object?
Today (2.5 years after this answer) you can safely use Array.forEach. As #ricosrealm suggests, decodeURIComponent was used in this function.
function getJsonFromUrl(url) {
if(!url) url = location.search;
var query = url.substr(1);
var result = {};
query.split("&").forEach(function(part) {
var item = part.split("=");
result[item[0]] = decodeURIComponent(item[1]);
});
return result;
}
actually it's not that simple, see the peer-review in the comments, especially:
hash based routing (#cmfolio)
array parameters (#user2368055)
proper use of decodeURIComponent and non-encoded = (#AndrewF)
non-encoded + (added by me)
For further details, see MDN article and RFC 3986.
Maybe this should go to codereview SE, but here is safer and regexp-free code:
function getJsonFromUrl(url) {
if(!url) url = location.href;
var question = url.indexOf("?");
var hash = url.indexOf("#");
if(hash==-1 && question==-1) return {};
if(hash==-1) hash = url.length;
var query = question==-1 || hash==question+1 ? url.substring(hash) :
url.substring(question+1,hash);
var result = {};
query.split("&").forEach(function(part) {
if(!part) return;
part = part.split("+").join(" "); // replace every + with space, regexp-free version
var eq = part.indexOf("=");
var key = eq>-1 ? part.substr(0,eq) : part;
var val = eq>-1 ? decodeURIComponent(part.substr(eq+1)) : "";
var from = key.indexOf("[");
if(from==-1) result[decodeURIComponent(key)] = val;
else {
var to = key.indexOf("]",from);
var index = decodeURIComponent(key.substring(from+1,to));
key = decodeURIComponent(key.substring(0,from));
if(!result[key]) result[key] = [];
if(!index) result[key].push(val);
else result[key][index] = val;
}
});
return result;
}
This function can parse even URLs like
var url = "?foo%20e[]=a%20a&foo+e[%5Bx%5D]=b&foo e[]=c";
// {"foo e": ["a a", "c", "[x]":"b"]}
var obj = getJsonFromUrl(url)["foo e"];
for(var key in obj) { // Array.forEach would skip string keys here
console.log(key,":",obj[key]);
}
/*
0 : a a
1 : c
[x] : b
*/
You could get a JavaScript object containing the parameters with something like this:
var regex = /[?&]([^=#]+)=([^&#]*)/g,
url = window.location.href,
params = {},
match;
while(match = regex.exec(url)) {
params[match[1]] = match[2];
}
The regular expression could quite likely be improved. It simply looks for name-value pairs, separated by = characters, and pairs themselves separated by & characters (or an = character for the first one). For your example, the above would result in:
{v: "123", p: "hello"}
Here's a working example.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I get query string values?
ive a a function call like:
var ex = _.where(obj, {param1:0,param2:1});
which works fine, all the code behind.
I check my URL and take out the parameters and save this information into a array:
var query = window.location.search.substring(1);
var vars = query.split("&");
var testParamter = vars.toString().replace( /=/g,':' );
that return by using console.log(testParamter); the following result:
param1:0,param2:1
but i cant insert yet the var testParamter into my function call:
var ex = _.where(obj, {testParamter});
because it is a string and i cant handel it with eval(). so can anyone tell me please the right way to solve it?
thanks
Iterate over the values from the querystring and create an object from those values :
var query = window.location.search.substring(1),
vars = query.split("&"),
params = {};
$.each(vars, function(k,v) {
var arr = v.split('=');
params[ decode(arr[0]) ] = decode(arr[1]);
});
var ex = _.where(obj, params);
function decode(s) {
return decodeURIComponent(s.replace(/\+/g, " "));
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Use the get parameter of the url in javascript
Suppose I have this url:
s = 'http://mydomain.com/?q=microsoft&p=next'
In this case, how do I extract "microsoft" from the string?
I know that in python, it would be:
new_s = s[s.find('?q=')+len('?q='):s.find('&',s.find('?q='))]
I use the parseUri library available here:
http://stevenlevithan.com/demo/parseuri/js/
It allows you to do exactly what you are asking for:
var uri = 'http://mydomain.com/?q=microsoft&p=next';
var q = uri.queryKey['q'];
// q = 'microsoft'
(function(){
var url = 'http://mydomain.com/?q=microsoft&p=next'
var s = url.search.substring(1).split('&');
if(!s.length) return;
window.GET = {};
for(var i = 0; i < s.length; i++) {
var parts = s[i].split('=');
GET[unescape(parts[0])] = unescape(parts[1]);
}
}())
Think this will work..