Click to See Complete Forum and Search --> : Javascript Help


GurusGuru
02-09-2003, 11:11 PM
Please could someone modify the following script (provided by jalarie)

<script type="text/javascript">
Now=new Date();
Out ='<img src="http://xyz.com/gren.gif';
Out+='?'+Now*1;
Out+='" onerror="this.src=\'mine/red.gif\'"/>\n';
document.write(Out);
</script>


Modification needed:

Adding a link (http://www.xyz.com target=_blank) to the image http://xyz.com/gren.gif.

And adding another link (mine/goonline.html target=_self) to the image mine/red.gif

GurusGuru
02-09-2003, 11:51 PM
well .....

Dave can't help either?

I really appreciate and thank Jalarie for providing the script.

Charles
02-10-2003, 12:05 AM
<script type="text/javascript">
<!--
document.write('<a href="http://www.xyz.com/" target="_top"><img src="http://www.xyz.com/gren.gif?', new Date().getTime(), '" target="_blank"></a>');
document.images[document.images.length-1].onerror = function () {this.onerror = function () {}; this.src = 'mine/red.gif'; document.links[0].href = 'http://www.w3.org/'; document.links[0].target="_self"}
// -->
</script>
<noscript><a href="mine/goonline.html"><img src="mine/red.gif"></a></noscript>

The tricky part is that you cannot call a link by its name in JavaScript. You will need to change document.links[0], which appears twice above, so that the index represents the number of links that precede this one on the page.

GurusGuru
02-10-2003, 01:11 AM
Thanks Charles,

The script is for an ebook which is being created of a Missing Children Website. There is a link 'Search for Missing Children' - search.html. On the website the search is done via a cgi script on a flat text database. Incase of an ebook the person viewing it will most probably be offline and conducting a search using a cgi script will not be possible. Hence a page is being developed for an ebook where when someone clicks on 'Search for Missing Children' he would be taken to a page containing the above script. If the gren.gif shows that means he is online and would be taken automatically/manually (by clicking) on the gren.gif and the search.html (http://www.xyz.com/search.html) page would open in a new window.

Incase he is offline, the red.gif will show that would take him to a page (mine/goonline.html) in the ebook. This page will have links to static pages of the Missing Children.

GurusGuru
02-10-2003, 01:16 AM
Originally posted by Dave Clark
Dave can, but Dave would have done it an entirely different way and Dave didn't think you'd like having the apple cart upset.

Dave

Well, Dave, don't mind having the apple cart upset. Let's have your different way.

Charles
02-10-2003, 08:15 AM
Yes, given the aim of the script, Dave's method is the one to use.

GurusGuru
02-10-2003, 12:26 PM
Thanks Dave and Charles.

Just 1 (major) problem. My knowledge about javascript is limited to Copy, Cut and Paste. Where and how do I stick the last few lines mentioned by you?

Charles
02-10-2003, 12:41 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Some Title</title>
<script type="text/javascript">
<!--
onLine = true;
myImg = new Image();
myImg.onerror = function() { onLine = false; }
myImg.src = "http://xyz.com/gren.gif?" + (new Date()).getTime();
// -->
</script>
</head>
<body>
<p><a href="http://www.xyz.com" target="_blank" onclick="if (!onLine) {this.url = 'mine/goonline.html'; this.target = '_self'}; return true"><img src="myimage.gif" border="0" alt="some_valid_alternative_text"></a></p>
</body>
</html>

GurusGuru
02-10-2003, 01:04 PM
Thanks Charles.

Whether I am online or offline it takes me to http://www.xyz.com.

If offline the url opens in the same frame and when online the url open in a new window.

Charles
02-10-2003, 01:11 PM
That doesn't sound possible; It can't half work. Please doble check and/or post your code.

GurusGuru
02-10-2003, 01:37 PM
Double checked. Maybe I am goofing up somewhere. The code is :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Some Title</title>
<script type="text/javascript">
<!--
onLine = true;
myImg = new Image();
myImg.onerror = function() { onLine = false; }
myImg.src = "http://www.ncmc.org/_images/ncmc.gif?" + (new Date()).getTime();
// -->
</script>
</head>
<body>
<p><a href="http://www.ncmc.org" target="_blank" onclick="if (!onLine) {this.url = 'goonline.htm'; this.target = '_self'}; return true"><img src="images/find.gif" border="0" alt="Click here"></a></p>
</body>
</html>

Charles
02-10-2003, 01:55 PM
I see the problem, we had a little neuron mis-fire there.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/ strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Some Title</title>
<script type="text/javascript">
<!--
onLine = true;
myImg = new Image();
myImg.onerror = function() {onLine = false}
myImg.src = "http://xyz.com/gren.gif?" + new Date().getTime();
// -->
</script>
</head>
<body>
<p><a href="http://www.xyz.com" target="_blank" onclick="if (!onLine) {this.href = 'mine/goonline.html'; this.target = '_self'}; return true"><img src="myimage.gif" border="0" alt="some_valid_alternative_text"></a></p>
</body>
</html>

GurusGuru
02-10-2003, 10:38 PM
Thanks Charles & Dave.

PERFECT