Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Closed Thread
Using JavaScript to check if a file exists
Old 02-27-2007, 04:15 PM Using JavaScript to check if a file exists
Ultra Talker

Posts: 330
Trades: 0
Is there any way, by using JavaScript, to check if a certain file exists in a full URL's directory? I have an image of a green light and an image of a red light. If the file exists in the directory I would like the green light to be shown, if not, then I would like the red light to be shown. Is this possible and can anyone help me out with this? All I need is the most simple form of the code determining whether it is found or not, then displaying either image. I think I would be able to take it from there.

Thanks in advance to anyone who can help me.
__________________

Please login or register to view this content. Registration is FREE
- 99.9% uptime web hosting. Set up an account within minutes.
JGiannotti is offline
View Public Profile
 
 
Register now for full access!
Old 02-27-2007, 04:46 PM Re: Using JavaScript to check if a file exists
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,938
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
Trades: 0
I would use a server-side programming language (e.g. ASP) to do this. People can disable/not use Javascript. They can't do that with ASP/PHP.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
(my blog)


Please login or register to view this content. Registration is FREE
(with proof)
ADAM Web Design is offline
View Public Profile Visit ADAM Web Design's homepage!
 
Old 02-27-2007, 04:49 PM Re: Using JavaScript to check if a file exists
Ultra Talker

Posts: 330
Trades: 0
Its just something I'm making as a personal project. It will be mostly used from my computer's local directories, so I'm keeping the whole page HTML and JavaScript only.
__________________

Please login or register to view this content. Registration is FREE
- 99.9% uptime web hosting. Set up an account within minutes.
JGiannotti is offline
View Public Profile
 
Old 02-27-2007, 04:58 PM Re: Using JavaScript to check if a file exists
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
No then it won't be possible.
For security reasons JavaScript doesn't have any functions for file handling.

Regards,
Insensus
Insensus is offline
View Public Profile
 
Old 02-27-2007, 05:08 PM Re: Using JavaScript to check if a file exists
Ultra Talker

Posts: 330
Trades: 0
I'm pretty sure it's possible though. I found this bit of coding, but I don't understand how to make it work.

if("cover.jpg" exist)
document.write('<img src='cover.jpg'>')
else
//display nothing
__________________

Please login or register to view this content. Registration is FREE
- 99.9% uptime web hosting. Set up an account within minutes.
JGiannotti is offline
View Public Profile
 
Old 02-27-2007, 05:12 PM Re: Using JavaScript to check if a file exists
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
That's no code.
It's an example of someone who doesn't know about JavaScript and hopes that this piece of fake coding illustrates good enough what he wants for someone to tell him how to do so.
It still isn't possible.

Regards,
Insensus
Insensus is offline
View Public Profile
 
Old 02-27-2007, 05:22 PM Re: Using JavaScript to check if a file exists
Ultra Talker

Posts: 330
Trades: 0
But what about this one: http://irt.org/script/52.htm

Is there any way I can use something similar to that, and check a file instead of an image?
__________________

Please login or register to view this content. Registration is FREE
- 99.9% uptime web hosting. Set up an account within minutes.
JGiannotti is offline
View Public Profile
 
Old 02-27-2007, 05:32 PM Re: Using JavaScript to check if a file exists
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
That's still not polling the file system. It's just attaching an error handler to the image tag. Not at all the same thing.

The other snippet is what's called "pseudo-code." Assuming it's what you're trying to do, though, why not just use the img tag, and accept the red X if the file doesn't exist?
Learning Newbie is offline
View Public Profile
 
Old 02-27-2007, 05:48 PM Re: Using JavaScript to check if a file exists
memberpro's Avatar
Super Talker

Posts: 143
Trades: 0
Javascript is called "client-side programming" for a reason... it can't manipulate the server files or client files... would you really want a javascript program deleting your hard drive while you were crusing the net?
__________________

Please login or register to view this content. Registration is FREE
- step-by-step learn how to design, create and install your own website in hours...not days.
Please login or register to view this content. Registration is FREE
was never so easy.
memberpro is offline
View Public Profile
 
Old 02-27-2007, 11:04 PM Re: Using JavaScript to check if a file exists
willcode4beer's Avatar
Super Moderator

Posts: 1,533
Name: Paul Davis
Location: San Francisco
Trades: 1
Quote:
Originally Posted by JGiannotti View Post
Its just something I'm making as a personal project. It will be mostly used from my computer's local directories, so I'm keeping the whole page HTML and JavaScript only.
Since this is the case, then yes you absolutely can.
On windows systems you can create what is called an HTA (HyperText Application) which is escentially an HTML file (with an HTA extension) where your javascript has full system access, files, registry, network, etc... You just access those items through built in ActiveX controls. The MSDN site can give you all you need, just google the term.

On Linux, w/ KDE, you can also do something very similar as well as remote control other applications through the DCOP sytem.

I'm not sure how you'd do it on a Mac. But, since their claim to fame is usability, I'm sure there is something.

Last edited by willcode4beer; 02-27-2007 at 11:05 PM..
willcode4beer is offline
View Public Profile
 
Old 02-28-2007, 12:38 AM Re: Using JavaScript to check if a file exists
Ultra Talker

Posts: 330
Trades: 0
Maybe I was being a bit vague.

I plan on using the file on my computer, to check up on various sites on the internet which I don't have access to. I just need the JavaScript to check up on a URL and simply give me a yes or no if it can access it.
__________________

Please login or register to view this content. Registration is FREE
- 99.9% uptime web hosting. Set up an account within minutes.
JGiannotti is offline
View Public Profile
 
Old 03-03-2007, 05:37 AM Re: Using JavaScript to check if a file exists
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
you can use the HttpRequest Object to retrieve the HTTP Response for the document.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
View Public Profile Visit chrishirst's homepage!
 
Old 11-03-2007, 09:00 PM Re: Using JavaScript to check if a file exists
Junior Talker

Posts: 1
Trades: 0
Its not possible for a file without resorting to an Ajax call or some other mechanism to hit the server. But, just for information sake if the file is an image then you can check by using the onError attribute on an image tag. For example;

<img src="http://www.somehost.com/myimage.jpg" onerror="alert('Image missing')">

See http://www.irt.org/script/52.htm for more

I know the original post was a while ago, but as this is still high up when you google this topic, thought I would post to help others.

Last edited by hoegaarth; 11-03-2007 at 09:04 PM..
hoegaarth is offline
View Public Profile
 
Old 11-06-2007, 04:28 PM Re: Using JavaScript to check if a file exists
rohan_india's Avatar
Novice Talker

Posts: 9
Name: Rohan Shenoy
Location: Thane-Vashi,Mumbai,India
Trades: 0
Yes, checking links is possible if you have the following:

1. Custom error pages: Directions to create custom error pages can be found at http://en.wikipedia.org/wiki/Custom_error_page and http://www.outfront.net/tutorials_02...tomerrors1.htm

2. Opera browser, would be more productive that Internet Explorer or Firefox because IE asks for confirmation every time a page tries to close itself.

What to do: In custom error page, add the below code to close the window automatically.
Code:
<script>
window.close()
</script>
Now paste the below code in a blank text file and save it as .html file.
Now open that html file in your browser and start checking links.
Code:
<html>

<head>
<script>
function check()
{
webAdd=prompt("Enter url here in given format: www.google.com","www.google.com")
{
window.open("http://"+webAdd+"")
}
}
</script>
</head>

<body>
<input type="button" value="Check URL" onclick="check()">
</body>

</html>
If you are going to use it on localhost, you can customize the webAdd that way so that you will need to enter only the file name at prompt box.

Rationale behind this trick:
If file exists > Fill will load
If file does not exist > Custom error page loads > Custom error page closes due to inserted javascript code > User returns to the window in which he was previously there(i.e,-URL checking page)

So If you return to the file checking page, it means file does not exist.
__________________
Medical entrance resources for MHT-CET:
Please login or register to view this content. Registration is FREE


My javascript showcase:
Please login or register to view this content. Registration is FREE

Last edited by rohan_india; 11-07-2007 at 01:06 AM.. Reason: add a missing point
rohan_india is offline
View Public Profile Visit rohan_india's homepage!
 
Closed Thread     « Reply to Using JavaScript to check if a file exists
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.77583 seconds with 12 queries