Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split query string, +, = and &

Status
Not open for further replies.

DylanTz

Technical User
Joined
Jul 14, 2003
Messages
10
Location
GB
Hi

This script splits the query string, leaving only the "my+query" for use...., however, can anyone suggest how I can split the QS so that the result is "my" "query"

TIA

<script>
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars.split("=");
if (pair[0] == variable) {
return pair[1];
}
}
}
</script>
 
either use the split function one more time (not sure why you can't figure it out since you're already using it twice) or use the urldecode function.



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Sorry, its not my script!, I found it online...., im just learning.

By that you mean, duplicate the function, and change it as: -

<script>
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("+");
for (var i=0;i<vars.length;i++) {
var pair = vars.split("&");
if (pair[0] == variable) {
return pair[1];
}
}
}
</script>

so that it splits the +, and & of the QS?
 
>split the QS so that the result is "my" "query"

If by that you mean decode that part of "+", which is the urlencode of space, back to the space, you can add one line to the original function. The rest remains the same.
[tt]
var query = window.location.search.substring(1);
[blue]query=query.replace(/\+/g," ");[/blue]
[/tt]
 
This had no effect...

function getQueryVariable(variable) {
var query = window.location.search.substring(1);
query=query.replace(/\+/g," ");
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars.split("=");
if (pair[0] == variable) {
return pair[1];
}
}
}
 
Can I just clarify, I want to remove the =, + and & from the query string, i.e. srch=my+query&search....,

so that I am left with "my" "query"?
 
sorry, i want to remove the =, + and &, not from the actual URL in the browser!, but from the url so it can use in my javascript
 
Let's say you get the window.location.search emulated in s for testing. Is it that you want this behaviour? The rest, you've to figure out yourself.
[tt]
var t;
t=getQueryVariable("x");
alert(t);
t=getQueryVaariable("y");
alert(t);
t=getQueryVariable("anything");
alert(t);

function getQueryVariable(variable) {
[green]//var query = window.location.search.substring(1);[/green]
[blue]var s="?x=a&y=b+c&z=d+e"; //testing
var query;
query=s.substring(1);[/blue]
query=query.replace(/\+/g," ");
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars.split("=");
if (pair[0] == variable) {
return pair[1];
}
}
[blue]return ""; //return empty if so desired without match[/blue]
}
[/tt]
 
Hi

Thanks for the reply, thanks a lot, I apprecite you taking the time to look at it.

I'll test it later today, and post the results.

Thanks
 
Hi

Ok, I ran the script, as it was written by tsuji...

Firstly, on load, the script alerts the value "a"...

with error Object expected ...

When I make these changes to the script above, then the right side of the query string passes, but with the Object expected error:

var t;
t=getQueryVariable("x");
alert(t);
t=getQueryVaariable("y");
alert(t);
t=getQueryVariable("anything");
alert(t);

function getQueryVariable(variable) {
var query = window.location.search.substring(1);
//var s="?x=a&y=b+c&z=d+e"; //testing
var query;
query=query.substring(1);
query=query.replace(/\+/g," ");
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars.split("=");
if (pair[0] == variable) {
return pair[1];
}
}
return ""; //return empty if so desired without match
}
 
[1]
>[self]t=getQueryVaariable("y");
[tt]t=getQueryV[red]a[/red]riable("y");[/tt]
[2]
If you want to comment out the testing part I showed, do it completely.
[tt] var query = window.location.search.substring(1);
//var s="?x=a&y=b+c&z=d+e"; //testing
//var query;
//query=query.substring(1);
query=query.replace(/\+/g," ");[/tt]
[3]
This is a self-contained demo, just do submit to see the consequence.
[tt]
<html>
<head>
<script language="javascript">
function doit() {
var s=window.location.search;
alert(s);
getit();
}
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
/*
var s="?x=a&y=b+c&z=d+e";
var query;
query=s.substring(1);
*/
query=query.replace(/\+/g," ");
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars.split("=");
if (pair[0] == variable) {
return pair[1];
}
}
return ""; //return empty if so desired without match
}
function getit() {
var t;
t=getQueryVariable("x");
alert(t);
t=getQueryVariable("y");
alert(t);
t=getQueryVariable("anything");
alert(t);
}
window.onload=doit;
</script>
</head>
<form name="formname" method="get">
<input type="text" name="x" value="a" /><br />
<input type="text" name="y" value="b c" /><br />
<input type="text" name="z" value="d e" /><br />
<input type="submit" />
</form>
</html>
[/tt]
 
Hei there...Tsuji...How are you. Thanks

Still looking at it right now, and I am just trying to figure it out, I have the values being alerted, and that these value are being passed. I think I'll get it soon.

Will post a little later
 
OK, how can I split the value of the query string into 2 variables as so that I can make reference to them when a search is conducted.

I see that query=query.replace(/\+/g," "); removes the + from the QS, as traced in the alert, but now it seems that I need to assign a variable to each section of the QS.

For example!

(getQueryVariable("left_par_of_QS"));

AND

(getQueryVariable("Right_part_of_QS"));

SUCH AS(getQueryVariable("left_par_of_QS") +" "+ getQueryVariable("Right_part_of_QS"));

This is the JS I have

<script language="javascript">
function doit() {
var s=window.location.search;
alert(s);
getit();
}
function getQueryVariable(variable) {
var query = window.location.search.substring(1);

//var s="?search=a&y=b+c&z=d+e";
//var query;
//query=s.substring(1);

query=query.replace(/\+/g," ");
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars.split("=");
if (pair[0] == variable) {
return pair[1];
}
}
return ""; //return empty if so desired without match
}
function getit() {
var t;
t=getQueryVariable("search");
alert(t);
t=getQueryVariable("y");
alert(t);
t=getQueryVariable("z");
alert(t);
}
//window.onload=doit;
</script>

The onload has been added to the body tag, since I use an onload already...
 
I believe somebody else can help on additional detail. I have done my part and I believe I've shown you the essential and trickier part. You should be capable of resolving the rest which is no more difficult.
 
OK, thanks for your help Tsuji....all the best
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top