Click to See Complete Forum and Search --> : URL varibale
cmvia
11-26-2003, 12:30 PM
Hello all! I am sure that the answer to this one is simple, but I have beating my head against the desk trying to find the answer. :D
How do you reference a variable in a page's URL?
So, if a page opens with the following URL:
www.page.com/page.html?errorcode=1
how do i get the value of the errorcode variable?
Help would be greatly appreciated!
Thanks!
Christine :D
TheBearMay
11-26-2003, 12:45 PM
Try this:
var ValuePassed;
var string1=document.location+"?";//add an extra ? to the end
slen=(string1.length) - 1;//set the ending point to 1 less (the added ?) than the length
inx=string1.indexOf("?");//find first occurance of ?
if (inx!=slen){ // check to make sure data was passed
ValuePassed=string1.substring(inx+1,slen);
...
cmvia
11-26-2003, 12:49 PM
Oh, thank you for your answer, but I think I may have been vague.
What I want to do is get that 1. (it can be a one or 2)
so my code would be something like this:
if (url.errorcode==1)
{
do some junk
}
else
{
do some other junk
}
my problem is, though, how do I say...url.errorcode?
Thanks!
ps. i have to stick to a strictly .html file. It is a template that cannot be changed...i can just add to it!
cmvia
11-26-2003, 12:50 PM
oh, okay. i understand what you said a little better now. but it still stands that i can't use php. any more ideas?
Thanks!
Snore
11-26-2003, 12:51 PM
Im trying to do something similar (and im to lazy to make a new thread :D)
Ive got a wee javascript thang in which i have a variable that contains an url, is there any way i could get javascript to make a link to the contents of that variable?
Any help appreciated
Jeff Mott
11-26-2003, 02:59 PM
How do you reference a variable in a page's URL?/******************************************
var pqs = new ParsedQueryString();
pqs.param( name )
Returns the string value associated with name (name is case sensitive). If
name was not defined in the query string, returns undefined. If multiple
values were associated with name, returns the first value from the order it
occurs in the query string.
e.g., QUERY-STRING: "hello=world&hello=hi&foo=bar"
pqs.param('hello') returns "world"
pqs.param('foo') returns "bar"
pqs.param('not there') returns undefined
pqs.params( [ name ] )
Returns an array for all values associated with name (name is case
sensitive). If name was not defined in the query string, returns null. If
name is omitted, returns an array of every name in the query string.
e.g., QUERY-STRING: "hello=world&hello=hi&foo=bar"
pqs.params('hello') returns ["world", "hi"]
pqs.params('foo') returns ["bar"]
pqs.params('not there') returns null
pqs.params() returns ["hello", "foo"]
*****************************/
String.prototype.decodeURL = function() {
return unescape(this.replace(/\+/g, " "));
}
function ParsedQueryString()
{
var parameters = {};
parse();
function parse() {
var parts = window.location.search.substr(1).split(/[&;]/);
for (var i = 0; i < parts.length; ++i) {
var pair = parts[i].split(/=/);
var name = pair[0].decodeURL();
var value = pair[1] != undefined ? pair[1].decodeURL() : undefined;
if (parameters[name] == undefined)
parameters[name] = [value];
else
parameters[name].push(value);
}
}
this.param = function(name) {
return parameters[name] != undefined ? parameters[name][0] : undefined;
}
this.params = function(name) {
if (arguments.length)
return parameters[name] != undefined ? parameters[name] : null;
else {
var pnames = [];
for (var p in parameters)
pnames.push(p);
return pnames;
}
}
}
cmvia
11-28-2003, 06:49 AM
Thanks so much for your reply and help!!
Have a good week-end!