ch*********@gmail.com said the following on 1/7/2007 8:20 PM:
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>