Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-04-2004, 01:04 AM   PM User | #1
Capt_Ron
New to the CF scene

 
Join Date: Feb 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Capt_Ron is an unknown quantity at this point
File Checker HELP

I have a script that generates a filename from form elements.
I need to add some error correcting to it.

The script concatonates (spelled right?) using 3 variables from the form and 2 strings of text to form a path. I need to check the results of that against a folder to make sure the filename created actually exists before directing the browser to display the file. If the file does not exist I want a pop-up message to appear saying so and refreshing the page so the user can re-enter the request.

Here is my original script:

---------BEGIN CODE HERE--------------
<script language="javascript">
function buildLink()
{
var pdfLink
pdfLink = '../menus/'
pdfLink += document.form1.mnuMeal.value;
pdfLink += document.form1.mnuDay.value;
pdfLink += document.form1.mnuMonth.value;
pdfLink += '.pdf',

document.form1.action = pdfLink;
document.form1.submit();
}
</script>

<form name=form1>
<select name=mnuMeal>
<option value="Bre">Breakfast</option>
<option value="Lun">Lunch</option>
</select>
<select name=mnuDay>
<option value="Wek">Weekday</option>
<option value="End">Weekend</option>
</select>
<select name=mnuMonth>
<option value="Jan">Jan</option>
<option value="Feb">Feb</option>
<option value="Mar">Mar</option>
...
</select>
<input type=button value="View Menu" onclick="buildLink();">
</form>
---------END CODE HERE--------------
Capt_Ron is offline   Reply With Quote
Old 02-04-2004, 02:44 AM   PM User | #2
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
You have to send request to the server to check if the file exists unless you have already loaded the list to javascript. But you can also do it purely client-side by using the XMLHTTP object.
Since you only need to check if the file exists, just change the line in that script:

return oxmlhttp.responseText;

to:

return (oxmlhttp.status==200) ? true:false;

which means the function returns true if the file exists and false otherwise.

And I noticed that you're accessing the value of the comboboxes using comboboxRef.value. Only few browsers support that. This is the cross-browser way:

var pdfLink = '../menus/';
var f = document.form1;
pdfLink += f.mnuMeal.options[f.mnuMeal.selectedIndex].value;
pdfLink += f.mnuDay.options[f.mnuDay.selectedIndex].value;
pdfLink += f.mnuMonth.options[f.mnuMonth.selectedIndex].value;

or better, pass the form reference to the function

function buildLink(f)
{
var pdfLink = '../menus/';
pdfLink += f.mnuMeal.options[f.mnuMeal.selectedIndex].value;
pdfLink += f.mnuDay.options[f.mnuDay.selectedIndex].value;
pdfLink += f.mnuMonth.options[f.mnuMonth.selectedIndex].value;
...
}
...
<input type="button" value="View Menu" onclick="buildLink(this.form);">
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 02-04-2004, 03:04 AM   PM User | #3
Capt_Ron
New to the CF scene

 
Join Date: Feb 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Capt_Ron is an unknown quantity at this point
Glenn,

Please forgive my ignorance.
How does the page reference the XMLHTTP object? I've never used that before. (Just now learning XML)

I like the function idea. I'll make that change in the morning.

I'm real new to javascript and need a little more clarity.

Thank you very much for your time.
Ron.
Capt_Ron is offline   Reply With Quote
Old 02-04-2004, 05:19 AM   PM User | #4
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
You don't have to know XML as the name implies. Actually, you can access any type of file (including pdf files). Just use the function from the link I posted and modify the last line as I previously said.
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 02-04-2004, 02:39 PM   PM User | #5
Capt_Ron
New to the CF scene

 
Join Date: Feb 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Capt_Ron is an unknown quantity at this point
Glenn,

OK. I have the function on the page. How do I use the result?

I can now check to see if the file exists. I need to display a message if the result is false.

Thank you for your patience.

Ron.
Capt_Ron is offline   Reply With Quote
Old 02-05-2004, 01:35 AM   PM User | #6
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
Code:
function buildLink(f)
{
  var pdfLink = '../menus/';
  pdfLink += f.mnuMeal.options[f.mnuMeal.selectedIndex].value;
  pdfLink += f.mnuDay.options[f.mnuDay.selectedIndex].value;
  pdfLink += f.mnuMonth.options[f.mnuMonth.selectedIndex].value;
  var exists = getFile(pdfLink);
  if (exists){
    location.href=pdfLink;
  }
  else alert('File "' + pdfLink + '" does not exist.');
}
...
<input type="button" value="View Menu" onclick="buildLink(this.form);">
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 02-05-2004, 08:33 PM   PM User | #7
Capt_Ron
New to the CF scene

 
Join Date: Feb 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Capt_Ron is an unknown quantity at this point
Glenn,
Here is my final text. I keep getting a false on the getFile function.
Any ideas?
PS: the filename and path are correct.

---- SCRIPT ----
<script language="javascript">
function buildLink(f)
{
var pdfLink = '../menus/';
pdfLink += f.mnuLevel.options[f.mnuLevel.selectedIndex].value;
pdfLink += f.mnuMeal.options[f.mnuMeal.selectedIndex].value;
pdfLink += f.mnuMonth.options[f.mnuMonth.selectedIndex].value;
pdfLink += '.pdf';
var exists = getFile(pdfLink);
if (exists){
location.href=pdfLink;
}
else alert('Menu not available yet, please try again later."' + pdfLink);
}

function getFile(filename)
{ oxmlhttp = null;
try
{ oxmlhttp = new XMLHttpRequest();
oxmlhttp.overrideMimeType("text/xml");
}
catch(e)
{ try
{ oxmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{ return null;
}
}
if(!oxmlhttp) return null;
try
{ oxmlhttp.open("GET",filename,false);
oxmlhttp.send(null);
}
catch(e)
{ return null;
}
return (oxmlhttp.status==200) ? true:false;
}

</script>

---- FORM SUBMIT LINE ----

<input type="button" value="View Menu" onclick="buildLink(this.form);">

Last edited by Capt_Ron; 02-05-2004 at 08:35 PM..
Capt_Ron is offline   Reply With Quote
Old 02-06-2004, 02:21 AM   PM User | #8
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
try putting absolute url

var pdfLink = 'http://mydomain.com/menus/';

Also try inserting alerts at some points in the function to verify values.

Can you tell me what does oxmlhttp.status return?
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:52 PM.


Advertisement
Log in to turn off these ads.