PDA

View Full Version : Get string out of location url



marringi
05-08-2007, 09:51 PM
I got a CGI/Perl script that reads databases and displays the results on template pages.
I need to be able to get the name of the database that is being read from the url location to be able to use it in a link in the template page.
The URL would look somehting like this:
perl.cgi?i=database&w=template&re=date&rada=012
in witch I would like to get the i field from the URL.

I heard an idea about creating a dummy <span> tag with an id, then within the span tag use document.write to write the location url into the span tag, then using the getElementById it might be possible to use substr(value1,value2) to identify the database call in the url.
I just don't know how to do this if it works.

Any ideas about what I could do to solve this problem?

mburt
05-08-2007, 11:25 PM
String.prototype.getvar = function(query) {
var value = this.substring(this.indexOf(query)+1,this.length),
end = value.indexOf("&") ? value.indexOf("&") : value.length;
return value.substring(value.indexOf(query)+query.length+1,end);
};
This basically works like this:
String.getvar("variable")

document.write("perl.cgi?i=database&w=template&re=date&rada=012".getvar("i"));
The above can be i, w, re or rada. Those are the variables in the string given.

mwinter
05-09-2007, 12:20 PM
String.prototype.getvar = function(query) {
var value = this.substring(this.indexOf(query)+1,this.length);
end = value.indexOf("&") ? value.indexOf("&") : value.length;
return value.substring(value.indexOf(query)+query.length+1,end);
};


That is very fragile code. I posted a better alternative a couple of days ago. It will need to be modified slightly to process the correct string (that version works on the Referrer URL).

Mike

mburt
05-09-2007, 10:43 PM
Oh, I never thought of using regular expressions... Neat.

jscheuer1
05-10-2007, 05:00 AM
I had seen mwinter's method of doing this a little earlier and came up with this version of it that allows you to optionally specify the string to be searched. It defaults to the location.search of the current page. Otherwise, it is pretty much a clone of mwinter's with a little error checking thrown in:


function getQval(n, m) {
if(!arguments[0]||typeof n!='string')
return null;
var r=new RegExp('[?&;]'+n+'=([^&;#]*)'), m=arguments[1]?m:location.search;
return (m=r.exec(m))? unescape(m[1]) : null;
}