Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 15th, 2004, 03:07 PM
Authorized User
 
Join Date: Mar 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default Me Again... Keywords: Javascript, File Exist

Ok, Im in the middle of creating a rollover image function (blah blah), but its dependant on the name of the image. Such as if an item # is: 93905 then the image would be something like 93905.jpg.

What I'm wondering is, is there is a way to check if the file exists on the server... I've tried a couple of methods that I've found and they did not work. One attempt was:

    var sFileName = "http://www.fakepage.com/image/rollover/" + cleanString(productId, "+-./\\") + ".jpg";
    var oFso = new ActiveXObject("Scripting.FileSystemObject");
    if (oFso.fileExists(sFileName))
    {
        document.getElementById(hidVarValue).src = imageFile;
    }
    else
    {
        document.getElementById(hidVarValue).src = "http://www.fakepage.com/images/rollover/empty.jpg";
    }
    document.getElementById(hidVarValue + "link").href = infoLink;

This gave me an error in trying to create the oFso object with the "new ActiveXObject" call.

All I want to do is to know if the file exists, so that I may replace it with another image called empty.jpg to avoid having that annoying [X] image replacement when file is non-existant.

 
Old November 15th, 2004, 11:02 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

You cannot use JavaScript to perform tasks on the server. JavaScript is client-side ONLY (unless you use it in ASP).

I would use PHP, ASP, CGI or some other server language to perform this task.

-Snib
Where will you be in 100 years?
Try new FreshView 0.2!
 
Old November 16th, 2004, 04:47 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

This might give you some ideas (don't use with animated gifs etc.):
Code:
<html>
<head>
<title>Image Exists</title>
<script type="text/jscript">
  function doesImageExist(path)
  {
    var oImg = new Image();
    oImg.onload = function(){onImageEvent(oImg, true);};
    oImg.onerror = function(){onImageEvent(null, false)};
    oImg.src = path;    
  }

  function onImageEvent(img, loaded)
  {
    if (loaded)
    {
      alert("Width: " + img.width + "\nHeight: " + img.height);    
    }
    else
    {
      alert("Failed");    
    }
  }


</script>
</head>

<body bgcolor="#FFFFFF">
Enter path to image and press test. (Use / as path separator.) 
<input type="text" size="50" id="txtImagePath">&nbsp;<input type="button" value="Test" onclick="doesImageExist(txtImagePath.value);">
</body>
</html>
--

Joe (Microsoft MVP - XML)
 
Old November 16th, 2004, 04:46 PM
Authorized User
 
Join Date: Mar 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

see thats the thing Snib, Im used to using ASP bleh...

Anyway, the site that I was working with doesnt allow scripting, the company that Im working for at the moment, decided to use a websolution called flashecom, so what I was trying to do was to over-ride things with javascript.

Anywho, I figured out a way to get around this issue. I decided to use the onerror event handler through the image object to call a function that placed a default image called empty.jpg (1x1px). It seemed to work when the image is not found XD

Thanks guys for the info!






Similar Threads
Thread Thread Starter Forum Replies Last Post
The file '/FrontPageMaster.master' does not exist. estranda ASP.NET 2.0 Professional 0 January 5th, 2008 01:28 PM
Keywords on .CSS ? AMB CSS Cascading Style Sheets 1 May 27th, 2006 09:15 PM
use of Not and IS Nothing keywords jay schumacher VB.NET 2002/2003 Basics 1 March 31st, 2006 10:01 PM
highlight search keywords allang JSP Basics 2 October 13th, 2004 01:09 AM
META KEYWORDS anshul HTML Code Clinic 6 September 11th, 2004 06:06 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.