Connecting Tech Pros Worldwide Help | Site Map

Display HTML when file is found

chadschultz@gmail.com
Guest
 
Posts: n/a
#1: Jan 8 '07
Hi there! A friend of mine asked about how to have a line of HTML be
written based on whether or not a certain file exists in the server. So
if the file is there, a line of HTML is printed; if the file is not
there, the HTML is not written and the browser continues on its merry
way through reading the rest of the page.

I know, of course, that server-side languages such as PHP, etc. would
have no difficulty with this. But can javascript (or any other method)
do this without requiring changes to the server? Thanks!

Randy Webb
Guest
 
Posts: n/a
#2: Jan 8 '07

re: Display HTML when file is found


chadschultz@gmail.com said the following on 1/7/2007 8:20 PM:
Quote:
Hi there! A friend of mine asked about how to have a line of HTML be
written based on whether or not a certain file exists in the server. So
if the file is there, a line of HTML is printed; if the file is not
there, the HTML is not written and the browser continues on its merry
way through reading the rest of the page.
>
I know, of course, that server-side languages such as PHP, etc. would
have no difficulty with this. But can javascript (or any other method)
do this without requiring changes to the server? Thanks!
You could check the existence of the file using HTTPRequestObject and
write the text accordingly.

<URL: http://jibbering.com/2002/4/httprequest.html>

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
zero0x
Guest
 
Posts: n/a
#3: Jan 8 '07

re: Display HTML when file is found


or you can also you XMLHttpRequest, but afaik it works only if the path
is on the server.

you can do this like this

function fileExists(file)
{
var xr = false;
if(window.XMLHttpRequest)
xr = new XMLHttpRequest();
else if(window.ActiveXObject)
xr = new ActiveXObject("Microsoft.XMLHTTP");

xr.open(file);
xr.onreadystatechange = function()
{
if(xr.readyState==4)
return xr.status;
}
xr.send(null);
}

this function returns http status like 200, or 404 etc.

Randy Webb
Guest
 
Posts: n/a
#4: Jan 8 '07

re: Display HTML when file is found


zero0x said the following on 1/8/2007 9:23 AM:
Quote:
or you can also you XMLHttpRequest, but afaik it works only if the path
is on the server.
That is about the most intuitive non-quoted reply I have seen in a long
time. Perhaps you should re-read the post you replied to.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
chadschultz@gmail.com
Guest
 
Posts: n/a
#5: Jan 9 '07

re: Display HTML when file is found


chadschultz@gmail.com said the following on 1/7/2007 8:20 PM:
Quote:
Quote:
Hi there! A friend of mine asked about how to have a line of HTML be
written based on whether or not a certain file exists in the server. So
if the file is there, a line of HTML is printed; if the file is not
there, the HTML is not written and the browser continues on its merry
way through reading the rest of the page.

I know, of course, that server-side languages such as PHP, etc. would
have no difficulty with this. But can javascript (or any other method)
do this without requiring changes to the server? Thanks!
>
You could check the existence of the file using HTTPRequestObject and
write the text accordingly.
>
<URL: http://jibbering.com/2002/4/httprequest.html>
>
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Thank you for the link! That helps a lot... but unfortunately, I'm not
able to make use of it. I've tried to test it and adapt it. I succumb
to complete quirkiness. The example has links with onclicks that call a
javascript function. When I tried to take the code out of the links and
put it in the document, so it would occur without being clicked, it
didn't want to work properly. It kept giving error messages for no
discernible reason that there was an invalid character. However, by
changing the number of calls to the function this error would disappear
or reappear... quite bizarre.

Anyway! I have modified it to an example page that I believe would suit
my purposes ideally. But even there, it's still quirky. As I played
with it for hours long into the night, I would seem to come up with no
results: a blank page (and complete source code), or one message
displayed (and no source code). Either the page produces no output, or
the message is displayed- and the source code shows nothing BUT the
message, no HTML or anything! In fact, this example uses one invalid
link and two invalid link; only the first valid link ever results in a
printed message. It's as if the code replaces the entire document with
that line and then stops. My goal is to have a fully functioning web
page and then simply have a few lines that print or not based on the
existence of a file- what am I doing wrong?



<html>
<head>
<title>SSJS Test</title>
<SCRIPT Language="JavaScript">
var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE
versions.
// and security blocked creation of the objects.
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp=false;
}
}
if (!xmlhttp && window.createRequest) {
try {
xmlhttp = window.createRequest();
} catch (e) {
xmlhttp=false;
}
}




function ifexists(url,msg) {
xmlhttp.open("HEAD", url,true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4)
if (xmlhttp.status==200) document.write(msg)
}
xmlhttp.send(null)
}
//-->
</script>


</head>

<body>
Blah blah blah blah
<p>

<SCRIPT Language="JavaScript">
<!--
ifexists("index.htm","Hello, World!")
//-->
</script>

<SCRIPT Language="JavaScript">
<!--
ifexists("invalid.htm","How's it going, World?")
//-->
</script>

<SCRIPT Language="JavaScript">
<!--
ifexists("valid.html","Goodbye, World!")
//-->
</script>




</body>
</html>

marss
Guest
 
Posts: n/a
#6: Jan 9 '07

re: Display HTML when file is found



chadschultz@gmail.com wrote:
Quote:
function ifexists(url,msg) {
xmlhttp.open("HEAD", url,true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4)
if (xmlhttp.status==200) document.write(msg)
}
xmlhttp.send(null)
}
//-->
</script>
>
>
</head>
>
<body>
Blah blah blah blah
<p>
>
<SCRIPT Language="JavaScript">
<!--
ifexists("index.htm","Hello, World!")
//-->
</script>
>
<SCRIPT Language="JavaScript">
<!--
ifexists("invalid.htm","How's it going, World?")
//-->
</script>
>
<SCRIPT Language="JavaScript">
<!--
ifexists("valid.html","Goodbye, World!")
//-->
</script>
>
1. If you using only XMLHttpRequest object then send next request just
after you have got result from previous one.
Or use a few XMLHttpRequest objects.
2. xmlhttp.status is 200 if page exists, otherwise - it equals 404.
3. If you don't want to overwrite the page your script executed on
don't use document.write.

Here is a variant of solution. Maybe it is that you want.

<html>
<head>
<script type="text/javascript">
var xmlhttp=false;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp=false;
}
}

if (!xmlhttp && window.createRequest) {
try {
xmlhttp = window.createRequest();
} catch (e) {
xmlhttp=false;
}

}

var pagesToTest = ["index.htm","invalid.htm","valid.html"];
var index = 0;
function ifexists()
{
xmlhttp.open("HEAD", pagesToTest[index],true);
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4)
{
var result;
if (xmlhttp.status==200)
result = pagesToTest[index] + " exists<br>";
else if (xmlhttp.status==404)
result = pagesToTest[index] + " does not exist<br>";

document.getElementById("divResults").innerHTML += result;

index++;
if (index < pagesToTest.length)
ifexists();

}
}
xmlhttp.send(null)
}

ifexists();

</script>
</head>
<body>
Blah blah blah blah
<div id="divResults"></div>
</body>
</html>

Closed Thread