PDA

View Full Version : using window.location



marringi
03-26-2008, 05:38 PM
hello,

The thing is, I'm using a perl database program and I am going print out many types of the database in one html file. This is no problem using <!--#include virtual="file.cgi?database=data" --> in a shtml file.

But what I am hoping to accomplish is that I could write in the window location: file.shtml?process=something. Something would be written instead of data. Then I wouldn't have to create I new shtml file for each database. I'm pretty sure that this isn't possible with JavaScript but I was hoping someone here could maybe help me, that is if it is possible.

Thank you

boogyman
03-26-2008, 05:53 PM
<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[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
alert('Query Variable ' + variable + ' not found');
}
</script>


Now make a request to page.html?x=Hello



<script>
alert( getQueryVariable("x") );
</script>



will allow you to gain access to the GET variables passed into the url address

marringi
03-27-2008, 01:59 AM
Thanks for the response. I'm not getting it to work. Do I use this with a .shtml file, or how do I use it?

Say I wanted to get this location put into the page file: script.cgi?database=nr1&search=..

and then and the x=hello query... how will do this?

I hope I'm making myself clear enough, so that we understand each other.

Nile
03-27-2008, 02:30 AM
From what I understand, just put it in your .shtml file, put it in the body section[I think], and everything should be fine, then do visit the address you'd go to:
*.shtml?x=Hello
I hope this helps. ;)

marringi
03-27-2008, 10:30 AM
Do you mean that i should do this:

<html>
<head>
<title></title>
<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[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
alert('Query Variable ' + variable + ' not found');
}
</script></head>
<body>


<!--#include virtual="database.cgi?database=one&x=
<script>alert( getQueryVariable("x") );</script>
" -->

</body>
</html>