javascript

The code below is working on my site.  See  http://www.sowter.co.uk/colours.php try testing it with say, 0250.
If you try a file which does not exist say 9999 it unsurprisingly  takes you to an Error 404 page.

Could somebody please write me some sample code which will test to see if the file exists....and if it does not will display a simple message preferably on the same page such as.  "File does not exist please contact support"  instead of being taken to the 404 page.

<SCRIPT LANGUAGE="JavaScript">
function testResults (form) {
var Value = form.inputbox.value;
window.open("http://www.sowter.co.uk/colours/"+Value+".pdf");
}
</SCRIPT>
 <FORM NAME="myform" ALIGN="LEFT" ACTION="" METHOD="GET">Enter 4 or 5 digit type number:  
<INPUT TYPE="text" NAME="inputbox" VALUE="">  
<INPUT TYPE="button" NAME="button" Value="CLICK HERE" onClick="testResults(this.form)">
</FORM>

Help much appreciated..I am not a very good programmer!
BrianSowterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MontoyaProcess Improvement MgrCommented:
just a quick question before we begin..  "Why not edit the 404 page? "
0
KimputerCommented:
Here is your code back. Also, I hate it when enter doesn't work, so I added that too!

<SCRIPT LANGUAGE="JavaScript"> 

    function submitenter(myfield,e)
    {
		
        var keycode;
        if (window.event) keycode = window.event.keyCode;
        else if (e) keycode = e.which;
        else return true;

        if (keycode == 13)
        {
            testResults(myfield);
		return false;
        }
        else
            return true;
    }
	
 function testResults (form) { 
 var Value = form.inputbox.value; 
 
 var request;
if(window.XMLHttpRequest)
    request = new XMLHttpRequest();
else
    request = new ActiveXObject("Microsoft.XMLHTTP");
request.open('GET', 'http://www.sowter.co.uk/colours/'+Value+'.pdf', false);
request.send(); // there will be a 'pause' here until the response to come.
// the object request will be actually modified
if (request.status === 404) {
    alert("The page you are trying to reach is not available.");
}
else {
 window.open("http://www.sowter.co.uk/colours/"+Value+".pdf"); 
}

 } 
 </SCRIPT> 
  <FORM NAME="myform" ALIGN="LEFT" ACTION="" METHOD="GET">Enter 4 or 5 digit type number:  
 <INPUT TYPE="text" NAME="inputbox" VALUE="" onkeypress="submitenter(this.form,event)">  
 <INPUT TYPE="button" NAME="button" Value="CLICK HERE" onClick="testResults(this.form)"> 
 
 </FORM>
 

Open in new window


Change error response in this line: alert("The page you are trying to reach is not available.");
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BrianSowterAuthor Commented:
Thank you so much for this. It works perfectly. The enter key bit is also brilliant!.   I moved the search option to the main Menu (under SEARCH). You can try it.  Your help is very much appreciated and will be appreciated by our customers too.

There is more more refinement which has been suggested.  Can the cursor appear in the box by itself?  is this easy to do?
0
KimputerCommented:
In your code, find and replace:

<body class="oneColElsCtrHdr" onLoad="document.myform.inputbox.focus();">

Open in new window

0
BrianSowterAuthor Commented:
Brilliant!  Thank you so much
Brian
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Internet / Email Software

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.