473,396 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Display HTML when file is found

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!

Jan 8 '07 #1
5 1417
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/
Jan 8 '07 #2
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.

Jan 8 '07 #3
zero0x said the following on 1/8/2007 9:23 AM:
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/
Jan 8 '07 #4
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>

Jan 9 '07 #5

ch*********@gmail.com wrote:
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>

Jan 9 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bart Van der Donck | last post by:
Jomo (jomo@dynamicdeveloper.co.uk) wrote: > That's great. Thanks. > Now what if I want to display the results within a > specific section of an existing HTML page? Not a problem. You could...
7
by: Winston | last post by:
Hi, I want to use the Webbrowser Control (the Internet Explorer Control) to display an XML-structure within a C# WinForm. The way the IE displays a XML-file in its native form is exactly what I...
7
by: Winston | last post by:
Hi, I want to use the Webbrowser Control (the Internet Explorer Control) to display an XML-structure within a C# WinForm. The way the IE displays a XML-file in its native form is exactly what I...
7
by: TLM | last post by:
I am trying to build a web application that will contain links to files on a users local computer. I am assuming that the files will be in a known location and can display in a browser window. ...
5
by: Martin Moser | last post by:
Does somebody knows, if theres a way to display any file (tiff, doc, pdf, .....) "inline" on an asp.net site? What I want to do is, to display a file, which is stored in the DB, and some...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.