Click to See Complete Forum and Search --> : Popup message if local file missing?


boardtc
09-19-2003, 09:07 AM
I am looking for a local solution only, this will be on a webpage loaded from the local machine onto the client browser.

The requirement is if a file exits on the machine when a user clicks on a link, I want the link to open as normal, if not I want a pop up message to display.

I believe on a server this would be possible with asp amd javascript however I don't think it's possible to use asp locally. Can anyone recommend a solution?

Thanks a lot,

Tom.

Khalid Ali
09-19-2003, 09:11 AM
search the forums,solutions to this question have been provided using ActiveXControl and Java

boardtc
09-19-2003, 11:37 AM
Originally posted by Khalid Ali
search the forums,solutions to this question have been provided using ActiveXControl and Java

Thanks for the post, I have searched the net and your forum for a solution without any luck, which is why I posted. I just spent another hour searching your forums after your mail. I can't find anything relevant. Some posts on PHP, is that what you mean?

Again, I stress, I am looking for a local solution, not one that requires a server. Can you please direct me to any relevant threads in your forum, I can't find them.

Thanks a lot, Tom.

Xin
09-19-2003, 02:35 PM
well, my idea could be silly but here it goes:
- for each of your local file, make a one-pixel gif file with the same name, say, u have a checkMe.html then make a one-pixel gif named checkMe.gif
- capture the onclick event
- when a link is clicked, check its href and load the its gif file with img.src=".." ... img = new Image()
- set img.onload to load the link, onerror to pop up message

boardtc
09-22-2003, 11:34 AM
Originally posted by Xin
well, my idea could be silly but here it goes:
- for each of your local file, make a one-pixel gif file with the same name, say, u have a checkMe.html then make a one-pixel gif named checkMe.gif
- capture the onclick event
- when a link is clicked, check its href and load the its gif file with img.src=".." ... img = new Image()
- set img.onload to load the link, onerror to pop up message

Great idea, thanks!

I tried

<html>
<head>
<title>Test</title>
<SCRIPT LANGUAGE="JavaScript">
function LoadJump() {
}

function ShowMessage() {
window.alert("Jump does not exist");
}
</SCRIPT>
</head>
<body>
<a href="?"><img src="jump.gif" onLoad="LoadJump()" onerror="ShowMessage()">Jump</a>
</body>
</html>

A dot shows before the link, maybe there's a way of getting rid of that. Anyway, not sure what to put in the href part or what to put in LoadJump? Any pointers would be much appreciated. Nice left field thinking.

Cheers, Tom.

Xin
09-22-2003, 12:19 PM
plz set up your pages as usual and include the following codes:


var testImg = new Image();
testImg.onload = loadLink;
testImg.onerror = showMessage;

document.onclick = checkClick;
if (document.captureEvents) {
document.captureEvents(Event.CLICK);
}

function checkLink(url) {
var imgPath = url.replace(/\.html$/i, '.gif');

// uncomment the following lines if you have file types like that
// imgPath = url.replace(/\.asp$/i, '.gif');
// imgPath = url.replace(/\.php$/i, '.gif');
// imgPath = url.replace(/\.htm$/i, '.gif');

testImg.page = url;
testImg.src = imgPath;
}

function checkClick(e) {
var who = null;

if (e) {
who = e.target;
}
else if (event) {
who = event.srcElement;
}

if (who != null && who.href) {
checkLink(who.href);
return false;
}
else if (e) {
e.target.handleEvent(e);
}

return true;
}

function loadLink() {
window.location.href = testImg.page;
}

function showMessage() {
window.alert(testImg.page + " does not exist.");
}



thx,

boardtc
09-22-2003, 04:43 PM
Thanks awfully for the code which I am trying to understand. I have test.html:

<html>
<head>
<title>Test</title>
<SCRIPT LANGUAGE="JavaScript">

var testImg = new Image();
testImg.onload = loadLink;
testImg.onerror = showMessage;

document.onclick = checkClick;
if (document.captureEvents) {
document.captureEvents(Event.CLICK);
}

function checkLink(url) {
var imgPath = url.replace(/\.html$/i, '.gif');
testImg.page = url;
testImg.src = imgPath;
}

function checkClick(e) {
var who = null;

if (e) {
who = e.target;
}
else if (event) {
who = event.srcElement;
}

if (who != null && who.href) {
checkLink(who.href);
return false;
}
else if (e) {
e.target.handleEvent(e);
}

return true;
}

function loadLink() {
window.location.href = testImg.page;
}

function ShowMessage() {
window.alert(testImg.page + " does not exist.");
}
</SCRIPT>
</head>
<body>
<a href=""><img src="jump.gif" onLoad="loadLink()" onerror="ShowMessage()">Jump</a>
</body>
</html>

When I load this in my browser (Firebird) I get the messagee "The file ./undefined cannot be found. Please check the location and try again". When I try in in IE the page url changes to the undefined page.

How can I stop the code executing straight away? Thanks for your patience.

Cheers, Tom.

Xin
09-22-2003, 04:50 PM
plz change the

function ShowMessage()

to

function showMessage()


and make your html codes like this (just as usual):

<a href="jump.html">Jump</a>


when the link is clicked, the js codes will check whether "jump.gif" can be loaded, if so, load the jump.html, if not, alert the message.

boardtc
09-22-2003, 05:07 PM
it's an ingenious solution!! My eyes are opening to the power of Javascript.

It works perfectly if jump.html exists. If it doesn't exist I get a popup saying "The file ./jump.html cannot be found. Please check the location and try again" rather than the customised message. I click ok and the same message appears which I can click ok on as well. In IE I get the page cannot be displayed page. The showMessage case is the same in both places now. it seems the onerror is not getting fired?

Thanks mate, Tom.

Xin
09-22-2003, 05:42 PM
well, plz change the line:

if (document.captureEvents) {

to

if (document.layers) {

this should solve the double alert in NS7 or Mozilla.


also change this line:

else if (e) {

to

else if (e && e.target && e.target.handleEvent) {

this should fixed the js error in Firebird when you click on the page but not on a link.


and to have this work, for each html file you will need a same named gif file. that's, if you have the jump.html but not jump.gif, it won't load jump.html for you, and if you have jump.gif but not jump.html, it will still try to load jump.html (remember i called it kind of silly idea?), because all the js codes do is to check the gif file first then load the html file.

thx,

boardtc
09-22-2003, 06:06 PM
Nice. The layers change fixed the double message, and the 2nd change worked for IE!

In my default browser, Firebird, I still get the same message rather than the custom one. Is this something to do with the way IE treats the file system?

Superb stuff. Thanks mate,

Tom.

<html>
<head>
<title>Test</title>
<SCRIPT LANGUAGE="JavaScript">

var testImg = new Image();
testImg.onload = loadLink;
testImg.onerror = showMessage;

document.onclick = checkClick;
if (document.captureEvents) {
document.layers(Event.CLICK);
}

function checkLink(url) {
var imgPath = url.replace(/\.html$/i, '.gif');
testImg.page = url;
testImg.src = imgPath;
}

function checkClick(e) {
var who = null;

if (e) {
who = e.target;
}
else if (event) {
who = event.srcElement;
}

if (who != null && who.href) {
checkLink(who.href);
return false;
}
else if (e && e.target && e.target.handleEvent) {
e.target.handleEvent(e);
}

return true;
}

function loadLink() {
window.location.href = testImg.page;
}

function showMessage() {
window.alert(testImg.page + " does not exist.");
}
</SCRIPT>
</head>
<body>
<a href="jump.html">Jump</a>
</body>
</html>

Xin
09-22-2003, 06:16 PM
well, u replaced the wrong line:

if (document.captureEvents) {
document.layers(Event.CLICK);
}


should actually be:

if (document.layers) {
document.captureEvents(Event.CLICK);
}


thx,

boardtc
09-22-2003, 06:27 PM
That was careless, sorry. I now tried that (see below). Is still works with IE as did the previous incorrect one but the same message exists as described above for firebird :-(

<html>
<head>
<title>Test</title>
<SCRIPT LANGUAGE="JavaScript">

var testImg = new Image();
testImg.onload = loadLink;
testImg.onerror = showMessage;

document.onclick = checkClick;
if (document.layers) {
document.captureEvents(Event.CLICK);
}
function checkLink(url) {
var imgPath = url.replace(/\.html$/i, '.gif');
testImg.page = url;
testImg.src = imgPath;
}

function checkClick(e) {
var who = null;

if (e) {
who = e.target;
}
else if (event) {
who = event.srcElement;
}

if (who != null && who.href) {
checkLink(who.href);
return false;
}
else if (e && e.target && e.target.handleEvent) {
e.target.handleEvent(e);
}

return true;
}

function loadLink() {
window.location.href = testImg.page;
}

function showMessage() {
window.alert(testImg.page + " does not exist.");
}
</SCRIPT>
</head>
<body>
<a href="jump.html">Jump</a>
</body>
</html>

Xin
09-22-2003, 06:32 PM
seems to be fine with my Firebird-0.6.1-Win32.

could you go Tools->JavaScript Console, clear all the error messages first then reload the page and test again?

boardtc
09-22-2003, 06:37 PM
I have the same firebird. I cleared the console messages. Reopened Firebird with the page, reloaded just to be sureand I still get the message. Bummer :-( Wonder what's difference about my setup. Hate these "bugs"! Thanks for all your help man, wicked. Cheers, Tom.

boardtc
09-26-2003, 04:55 AM
Got back to this after a couple of days away. And now it works fine :-)

Thanks for a brilliant solution. It saves me having 2 files with duplictae code now. Superb!

Cheers, Tom.