PDA

View Full Version : load specific page, else ALERT??


markgt
04-10-2004, 12:46 PM
Hi!

Im REALLY new at Javascript - been browsing these forums and reading up like crazy for ony two weeks, to try and learn enough for a project i'm working on.

THE QUESTION:

I want the user to be able to type in a number (eg: abc_123) that will launch a popup containing the page (eg: abc_123.htm). I've managed to get this right by adapting a password script(where password= pagename.htm). That was quite easy.

BUT now i need to do this: IF they type in a filename that doesnt exist, it must either load a page saying "the number you have entered..." or cause an ALERT to popup. I've read that one can customise error pages and upload them to the server, but then that page would appear for ANY other error on the site too.

Is there a way to do this? I would guess I need to use an IF (else) statement. Is there any way javascript could check to see if the page exists (then load it), otherwise popup a custom page or ALERT. I've inserted my code below if needed.

Any help would be MUCH appreciated!

(AN excited-about-javascript newbie)



------- CODE: ----------------

<html>
<head>

<script ="javascript">

function(doThis)
{
refvar = document.form1.refnum.value;
}
</script>
</head>


<body>

<form name="form1">
refnum:
<input type="text" id="refnum" name="refnum" value=""><BR>
</form>


<script type="text/javascript">

function findpage(refUrl)
{
refvar = document.form1.refnum.value;
refUrl = (refvar + ".htm")
var mypopup2=window.open((refUrl),"win4",'width=300,height=400');
mypopup2.focus();
}
</script>

<BR>
<BR>
<a href="#" onClick="findpage(); return false">GET MY PAGE</a>


</body>
</html>

Mr J
04-10-2004, 01:01 PM
Please try the following


<html>
<head>

<script type="text/javascript">

function findpage(refUrl){
refvar = document.form1.refnum.value;

if(refvar!="abc_123"){
alert("You have entered an invalid number")
}
else{
refUrl = (refvar + ".htm")

alert("This is the page that will open in a new window\n\n"+refUrl+"\n\ndelete this alert line in the script")

mypopup2=window.open((refUrl),"win4",'width=300,height=400');
mypopup2.focus();
}

}
</script>

</head>
<body>

<form name="form1">
refnum:
<input type="text" id="refnum" name="refnum" value=""><BR>
</form>
<BR>
<BR>
<a href="#" onClick="findpage(); return false">GET MY PAGE</a>


</body>
</html>

markgt
04-10-2004, 03:46 PM
Thanks MrJ

we're half way there!

There are going to be about 15 code numbers that the user could type in (and these will be changing from week to week). I was hoping there was a way i could use javascript to check the files in the directory and act accordingly.

I guess one way would be to put the 15 numbers in an array (?), and use javascript to check that way, but that means i'll have to keep changing the array every few days - which i'm trying to avoid. Could i place that array in an exernal .js file to make it easier to update?

But ideally i'd prefer not to have to update every few days...

Any ideas??

thanks

glenngv
04-12-2004, 01:57 PM
Try this:

function fileExists(path){
var fileRequest;
switch(typeof ActiveXObject){
case 'function':
fileRequest=new ActiveXObject('Microsoft.XMLHTTP');
break;
default:
fileRequest=new XMLHttpRequest;
fileRequest.overrideMimeType("text/xml");
}
try{
fileRequest.open('GET',path,false);
fileRequest.send('');
}catch(e){
return false;
}
return (fileRequest.status==404)?false:true;
}

and then call it like this:

function findpage()
{
var refUrl = document.form1.refnum.value + ".htm";
if (fileExists(refUrl)){
var mypopup2=window.open(refUrl,"win4",'width=300,height=400');
mypopup2.focus();
}
else alert('The file "'+refUrl+'" does not exist.');
}