Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 189
  • Last Modified:

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!
0
BrianSowter
Asked:
BrianSowter
  • 2
  • 2
1 Solution
 
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
 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now