Click to See Complete Forum and Search --> : Searching a list and going to a url


jparker12345
02-05-2003, 09:55 AM
I want a user to be able to type in a page number, hit submit, and have it take them to the URL associated with that page number.

ie.

if they type in 111, 112, or 113 it will take them to Page1.html, if they type in 114 or 115 it will take them to Page2.html, etc.

The page numbers and urls will all need to be defined in the script, cant use a database.

I can find tons of these scripts if I use a drop down box, but I want it to be a search... any help?

thanks!

-jp

gil davis
02-05-2003, 10:14 AM
For the specific examples you gave:

<head>
<script>
var i = 0;
var pgs = new Array();
pgs[i++] = new Array("111","Page1.htm");
pgs[i++] = new Array("112","Page1.htm");
pgs[i++] = new Array("113","Page1.htm");
pgs[i++] = new Array("114","Page2.htm");
pgs[i++] = new Array("115","Page2.htm");

function go(it) {
for (var i in pgs)
{if (pgs[i][0] == it.value) break;}
if (pgs[i][0] == it.value)
{alert("Going to " + pgs[i][1]);
window.location.href = pgs[i][1];}
else
{alert("Bad Input");}
}
</script>
</head>
<body>
<form onsubmit="return false">
<input name="t1" type="text"><br>
<input type="button" onclick="go(this.form.t1)" value=" Go! ">
</form>
</body>

jparker12345
02-05-2003, 10:24 AM
Most excellent, thanks a ton for you help, that is exactly what I was looking for!

-jason

jparker12345
02-05-2003, 10:28 AM
hmm, I will probably have a couple hundred entries, but for the most part 2-3 pages will link to 1 html page. Is it possible to set up more than one page to go to the link without having a new array for every single one?

ie.

pgs[i++] = new Array("111 112 113","Page1.htm");

and then have it search the array?

gil davis
02-05-2003, 11:38 AM
<head>
<script>
var i = 0;
var pgs = new Array();
pgs[i++] = new Array("111","112","113","Page1.htm");
pgs[i++] = new Array("114","115","115","Page2.htm"); // note all entries must be valid

function go(it) {
for (var i in pgs)
{if ((pgs[i][0] == it.value) || (pgs[i][1] == it.value) || (pgs[i][2] == it.value)) break;}
if ((pgs[i][0] == it.value) || (pgs[i][1] == it.value) || (pgs[i][2] == it.value))
{alert("Going to " + pgs[i][3]);
window.location.href = pgs[i][3];}
else
{alert("Bad Input");}
}
</script>
</head>
<body>
<form onsubmit="return false">
<input name="t1" type="text"><br>
<input type="button" onclick="go(this.form.t1)" value=" Go! ">
</form>
</body>

jparker12345
02-05-2003, 11:44 AM
Ah, wonderful.

Thanks again, you have been a huge help!

-jp

jparker12345
02-05-2003, 11:52 AM
For it to work there has to be 3 pages associated with it?

gil davis
02-05-2003, 11:54 AM
Yes, but they don't have to be different numbers.

pgs[i++] = new Array("100","100","100","Page0.htm");

is legal.

jparker12345
02-05-2003, 12:06 PM
Is there a way to check the number of items in the array, then when doing the search check it against all but the last one(actually i guess it doesnt matter if it checks the html page), and then when doing the webpage change, just use the last item?

sorry if i am pushing my luck...

gil davis
02-05-2003, 12:24 PM
<head>
<script language="JavaScript1.3">
var i = 0;
var pgs = new Array();
pgs[i++] = new Array("Page1.htm","111","112","113");
pgs[i++] = new Array("Page2.htm","114","115");
pgs[i++] = new Array("Page3.htm","116");

function go(it) {
var found = false;
for (var i=0; i<pgs.length; i++)
{for (var j=1; j<pgs[i].length; j++)
{if (pgs[i][j] == it.value)
{found = true;
break;}
}
if (found) {break;}
}
if (found)
{alert("Going to " + pgs[i][0]);
window.location.href = pgs[i][0];}
else
{alert("Bad Input");}
}
</script>
</head>
<body>
<form onsubmit="return false">
<input name="t1" type="text"><br>
<input type="button" onclick="go(this.form.t1)" value=" Go! ">
</form>
</body>

jparker12345
02-05-2003, 12:30 PM
You Completely. And totally. Rock.

No more changes this time, honest :)

thanks again,

-jason

jparker12345
02-06-2003, 02:30 PM
I lied. I am bad. I am evil.

Is it possible to have it check if its a pdf or html file, and if its a pdf open in a new window? If its html just works as normal...

var i = 0;
var pgs = new Array();
pgs[i++] = new Array("Page1.htm","111","112","113");
pgs[i++] = new Array("Page2.htm","114","115");
pgs[i++] = new Array("Page3.pdf","116");

function go(it) {
var found = false;
for (var i=0; i<pgs.length; i++)
{for (var j=1; j<pgs[i].length; j++)
{if (pgs[i][j] == it.value)
{found = true;
break;}
}
if (found) {break;}
}
if (found)
{window.location.href = pgs[i][0];}
else
{alert("Invalid page number.");}
}

gil davis
02-06-2003, 03:48 PM
(heavy sigh)

You owe me some money. Something like $40 an hour comes to mind.

<head>
<script language="JavaScript1.3">
var i = 0;
var pgs = new Array();
pgs[i++] = new Array("Page1.htm","111","112","113");
pgs[i++] = new Array("Page2.htm","114","115");
pgs[i++] = new Array("Page3.pdf","116");

function go(it) {
var found = false;
for (var i=0; i<pgs.length; i++)
{for (var j=1; j<pgs[i].length; j++)
{if (pgs[i][j] == it.value)
{found = true;
break;}
}
if (found) {break;}
}
if (found)
{alert("Going to " + pgs[i][0]);
if (pgs[i][0].toLowerCase().indexOf(".pdf") == -1)
{window.location.href = pgs[i][0];}
else
{pdf = window.open(pgs[i][0],"pdf","height=480,width=640,top=0,left=0,resizable=1");}
}
else
{alert("Bad Input");}
}
</script>
</head>
<body>
<form onsubmit="return false">
<input name="t1" type="text"><br>
<input type="button" onclick="go(this.form.t1)" value=" Go! ">
</form>
</body>

jparker12345
02-06-2003, 05:28 PM
Dang, now I feel guilty...you didnt have to do it. :(

How many hours did it take?

gil davis
02-06-2003, 08:28 PM
Just kidding.

Are you learning anything, or just using me? ;-)

jparker12345
02-06-2003, 08:53 PM
Bit of both :)

I have been playing with the code(and looking up some stuff) to figure out how what you are doing works. I understand most of it once I see it, but still cant seem to do it myself(i tried making some of the modifications myself first).

You really have been a tremendous help.

-jp