jaysolomon
asked on
location.search
I am or have not really ever had to use location.search before.
What i would like is for someone or everyone to educate me a lil bit on it.
Say i have a link like so
<a href="blah.html?id=1">Go</ a>
and (for example purposes) i have a menu on the destination page, and i would like to search the location, then use document.getElementById("m enu"+ id) to set the style's of the active link.
assuming this is a menu, the links would look like so
<a href="blah1.html?id=1" id="menu1">Go</a>
<a href="blah2.html?id=2" id="menu2">Go</a>
<a href="blah2.html?id=3" id="menu3">Go</a>
How would i go about searching the location and getting the id from the query str?
Do you have to use something like window.location.search.spl it("?") or what
TIA
jAy
What i would like is for someone or everyone to educate me a lil bit on it.
Say i have a link like so
<a href="blah.html?id=1">Go</
and (for example purposes) i have a menu on the destination page, and i would like to search the location, then use document.getElementById("m
assuming this is a menu, the links would look like so
<a href="blah1.html?id=1" id="menu1">Go</a>
<a href="blah2.html?id=2" id="menu2">Go</a>
<a href="blah2.html?id=3" id="menu3">Go</a>
How would i go about searching the location and getting the id from the query str?
Do you have to use something like window.location.search.spl
TIA
jAy
strURL = location.search
arrURL =strURL.split("?")
then arrURL[1] will give you the name value pairs. If you have multiple pairs, then you will have to do a further split on &
FtB
arrURL =strURL.split("?")
then arrURL[1] will give you the name value pairs. If you have multiple pairs, then you will have to do a further split on &
FtB
Another approach:
<html>
<head><title>Results</titl e>
<script language="JavaScript">
function getValues(){
var strSearch = location.search;
strSearch = strSearch.substring(1);
var arrNameValues = strSearch.split("&");
for(i=0;i<arrNameValues.le ngth;i++){
document.writeln(unescape( arrNameVal ues[i]));
}
}
</script>
</head>
<body onLoad="javascript:getValu es()">
</body>
</html>
<html>
<head><title>Results</titl
<script language="JavaScript">
function getValues(){
var strSearch = location.search;
strSearch = strSearch.substring(1);
var arrNameValues = strSearch.split("&");
for(i=0;i<arrNameValues.le
document.writeln(unescape(
}
}
</script>
</head>
<body onLoad="javascript:getValu
</body>
</html>
This doesn't use a location.search, but it should do the job.
<script language="JavaScript" type="text/javascript">
function splitURL(obj)
{
var hr = document.getElementById(ob j).href;
var myId = hr.substring(hr.indexOf("i d=")+3,hr. length)
return myId
}
</script>
Call the function by using a link ID as a parameter.
splitURL('menu1')
It will return the value of id in the href.
Hope this is what you want.
<script language="JavaScript" type="text/javascript">
function splitURL(obj)
{
var hr = document.getElementById(ob
var myId = hr.substring(hr.indexOf("i
return myId
}
</script>
Call the function by using a link ID as a parameter.
splitURL('menu1')
It will return the value of id in the href.
Hope this is what you want.
ASKER
How would i go about stripping (no not my clothes) the "menu=" out?
say i have this in place of your document.writeln
document.getElementById("m enu"+ id)
where id would be the actual value of menu=
Example
The addy bar looks like so
http://blah.com/blah.html?menu=2
i would then nee dto get the 2, and it would then set document.getElementById("m enu"+ id) to document.getElementById("m enu2")
say i have this in place of your document.writeln
document.getElementById("m
where id would be the actual value of menu=
Example
The addy bar looks like so
http://blah.com/blah.html?menu=2
i would then nee dto get the 2, and it would then set document.getElementById("m
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Jay,
location.search as you know gives the search attached to the URL. You can extract the info for the difeerent fields using regex. Since ? indcates the end of the URL I have just used location.href to extract stuff.
however for something like blah2.html?id=3&name=Bill
str=window.location.search
and then extract using somthing like this
Name=str.replace(/.*name=( [^&]*).*/, "$1")
alert(Name)
idNum=str.replace(/.*id=([ ^&]*).*/," $1")
alert(idNum)
location.search as you know gives the search attached to the URL. You can extract the info for the difeerent fields using regex. Since ? indcates the end of the URL I have just used location.href to extract stuff.
however for something like blah2.html?id=3&name=Bill
str=window.location.search
and then extract using somthing like this
Name=str.replace(/.*name=(
alert(Name)
idNum=str.replace(/.*id=([
alert(idNum)
here's something that gets the field and the value in it using location.search...
str1 = location.search
str2 = str1.split("?")[1]
qryfield = str2.split("=")[0]
qryval = str2.split("=")[1]
alert(qryfield+"="+qryval)
but it is much better to extract using location.href like this:
str1 = location.href
str2 = str1.split("?")[1]
qryfield = str2.split("=")[0]
qryval = str2.split("=")[1]
alert(qryfield+"="+qryval)
str1 = location.search
str2 = str1.split("?")[1]
qryfield = str2.split("=")[0]
qryval = str2.split("=")[1]
alert(qryfield+"="+qryval)
but it is much better to extract using location.href like this:
str1 = location.href
str2 = str1.split("?")[1]
qryfield = str2.split("=")[0]
qryval = str2.split("=")[1]
alert(qryfield+"="+qryval)
ASKER
Hey Jay,
Thanks a bunch!
Fritz the Blank
Thanks a bunch!
Fritz the Blank
In case you just need to resolve two set of key-value fields and you know the order of the fields, this method is simple:
var maske = /^.*?=(.*?)&.*?=(.*)$/;
var query = location.search;
var resultat = query.match(maske);
Hereafter:
resultat[0] contains the whole querystring
resultat[1] contains the value of the first key
resultat[2] contains the value of the second key
The expression 'maske' can be extended further for more key-vale fields by inserting '(.*?)&.*?=' accordingly
For three key-value fields the 'maske' will look like this:
var maske = /^.*?=(.*?)&.*?=(.*?)&.*?= (.*)$/;
var maske = /^.*?=(.*?)&.*?=(.*)$/;
var query = location.search;
var resultat = query.match(maske);
Hereafter:
resultat[0] contains the whole querystring
resultat[1] contains the value of the first key
resultat[2] contains the value of the second key
The expression 'maske' can be extended further for more key-vale fields by inserting '(.*?)&.*?=' accordingly
For three key-value fields the 'maske' will look like this:
var maske = /^.*?=(.*?)&.*?=(.*?)&.*?=
How about a function to get a querystring value based on knowing the name of the variable?
url = http://www.blahblah.html?x=#FFFFFF
var MyValue = fn_querystring('x');
Has anyone seen a function like this? I have one (see below) that is for some reason working with some values but not others. If someone can improve the function below, I would be very very happy:
function PageQuery(q) {
if(q.length > 1) this.q = q.substring(1, q.length);
else this.q = null;
this.keyValuePairs = new Array();
if(q) {
for(var i=0; i < this.q.split("&").length; i++) {
this.keyValuePairs[i] = this.q.split("&")[i];
}
}
this.getKeyValuePairs = function() { return this.keyValuePairs; }
this.getValue = function(s) {
for(var j=0; j < this.keyValuePairs.length; j++) {
if(this.keyValuePairs[j].s plit("=")[ 0] == s)
return this.keyValuePairs[j].spli t("=")[1];
}
return false;
}
this.getParameters = function() {
var a = new Array(this.getLength());
for(var j=0; j < this.keyValuePairs.length; j++) {
a[j] = this.keyValuePairs[j].spli t("=")[0];
}
return a;
}
this.getLength = function() { return this.keyValuePairs.length; }
}
function queryString(key){
var page = new PageQuery(window.location. search);
return unescape(page.getValue(key ));
}
url = http://www.blahblah.html?x=#FFFFFF
var MyValue = fn_querystring('x');
Has anyone seen a function like this? I have one (see below) that is for some reason working with some values but not others. If someone can improve the function below, I would be very very happy:
function PageQuery(q) {
if(q.length > 1) this.q = q.substring(1, q.length);
else this.q = null;
this.keyValuePairs = new Array();
if(q) {
for(var i=0; i < this.q.split("&").length; i++) {
this.keyValuePairs[i] = this.q.split("&")[i];
}
}
this.getKeyValuePairs = function() { return this.keyValuePairs; }
this.getValue = function(s) {
for(var j=0; j < this.keyValuePairs.length;
if(this.keyValuePairs[j].s
return this.keyValuePairs[j].spli
}
return false;
}
this.getParameters = function() {
var a = new Array(this.getLength());
for(var j=0; j < this.keyValuePairs.length;
a[j] = this.keyValuePairs[j].spli
}
return a;
}
this.getLength = function() { return this.keyValuePairs.length;
}
function queryString(key){
var page = new PageQuery(window.location.
return unescape(page.getValue(key
}
here's something i'd like to share to jAy: lol =)
<script>
function printSrc(paramName){
url = "http://domain.com/go.htm?x=Big%20Boy&y=Medium%20Boy&z=Small%20Boy";url = url + "&";
url = unescape(url.split(paramNa me+"=")[1] );if(url.i ndexOf("&" )!=-1){url =url.split ("&")[0];} document.w rite(url); }
</script>
<p>hello... <br /><script>printSrc("x");</ script>!</ p>
<p>hello... <br /><script>printSrc("y");</ script>!</ p>
<p>hello... <br /><script>printSrc("z");</ script>!</ p>
take care,
gam3r
<script>
function printSrc(paramName){
url = "http://domain.com/go.htm?x=Big%20Boy&y=Medium%20Boy&z=Small%20Boy";url = url + "&";
url = unescape(url.split(paramNa
</script>
<p>hello... <br /><script>printSrc("x");</
<p>hello... <br /><script>printSrc("y");</
<p>hello... <br /><script>printSrc("z");</
take care,
gam3r
ASKER
;)