How to create hyperlink that appears only if the target file is present?

T

The Dana's

I need to create a static HTML file that runs only on the local client PC.

On the page (say PageA.htm), I would like for a hypelink to another page
(Page B) to appear ONLY if the target file the hyperlink references is
present (PageB.htm).

So If I'm viewing page A, and Page B is present, then I want a hyperlink to
show up on Page A to navigate to Page B.

If Page B is not present, then no hypelink would appear for Page B.

Can someone post some code that does this?
 
C

Carter Smith

That's impossible with HTML only. You have to use a scripting language such
as PHP to check if a file exists or not. And to use PHP the user must have
PHP installed and go through a web-server such as Apache to view the page
correctly.

This is something that would only be done server side. Not client side.

Ben Kucenski
www.icarusindie.com
 
D

Dietmar Meier

The said:
I need to create a static HTML file that runs only on the local
client PC.

On the page (say PageA.htm), I would like for a hypelink to another
page (Page B) to appear ONLY if the target file the hyperlink
references is present (PageB.htm).

So If I'm viewing page A, and Page B is present, then I want a
hyperlink to show up on Page A to navigate to Page B.

If Page B is not present, then no hypelink would appear for Page B.

In browsers that implement XMLHttpRequests (current MSIE and Mozilla),
you can use it to validate links. An example document (here I only change
the background color, you can remove the link from the document instead)
is here: http://www.innoline-systemtechnik.de/dhgm/validlinks.html

Note: In older browsers, that do not implement try..catch, this script
will result in a syntax error. Browsers that do implement try..catch, but
not XMLHttpRequest, will state all links as unconfirmed.

ciao, dhgm
 
T

The Dana's

Thanks for all the help !

This worked:

---------------
Parent.htm
---------------

<html>
<head>
<title id="ParentChild">ParentChild</title>
</head>
<body><font face='Arial'>
<h1>This is the Parent</h1>
<ul>
<li><a href="child.htm">Go to Child</a></li>
</ul>
</html>

---------------
Child.htm
---------------

<html>
<head>
<title >Child</title>
</head>
<body><font face='Arial'>
<br><br><p><center>
<script language="javascript">
function masterWindow()
{
window.open("parent.htm", "_top");
}

var oDummy = new Image();
oDummy.src = "parent.htm";
var sURI = oDummy.src.replace(/[#?].*$/, "");

try {
var oHttpReq = window.ActiveXObject
? new ActiveXObject('Microsoft.XMLHTTP')
: window.XMLHttpRequest
? new XMLHttpRequest()
: null;
if (oHttpReq) {
oHttpReq.open('HEAD', sURI, false);
oHttpReq.send(null);

var iStat = oHttpReq.status;
if (iStat >= 400) {
var bShow = false;
} else {
var bShow = true;
}
}
}
catch(e) {
var bShow = false;
}

if (bShow) {
document.write("<form>");
document.write(" <input type='button' value='Go to Parent'
onClick='masterWindow()' />");
document.write("</form>");
}

</script>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top