Gagan said:
all I want to do is add the ability for an image to take the user
further down a page (href="#bottom").
Would you like it that when a user clicks on the image, a new
picture is displayed in the current page inside the
'placeholder' element?. And because the new image may be bigger
than the window size, you'd like to scroll to the bottom of the
image?
And if JS is not available, or the user's browser doesn't
support your script, navigate to the image anyway?
Anyone tell me how to do this?
Put an anchor below your image, then as the last line of your
onclick, change document.location to the anchor which can be
added thusly:
...
<img id="placeholder" src="" alt="">
<a name="bottom">Bottom of the image</a>
...
My code on an image:
<a
onclick="SwapImage('no1','','/images/1.gif','no2','','../../../images/common/2-up.gif',1);return
showPic(this);" href="/images/sample2.gif" title="Logo">
<a href="/images/sample2.gif"
title="Logo"
onclick="
SwapImage('no1','','/images/1.gif','no2','',
'../../../images/common/2-up.gif',1);
var x = showPic(this);
document.location = '#bottom';
return x;
"><img ...
Note that the 'return' is moved to after the document location
change.
<img
src="../../../images/common/2.gif" name="no2" width="27" height="33"
id="no2" /></a>
and the accompanying javscript:
function showPic (whichpic) {
if (document.getElementById) {
document.getElementById('placeholder').src = whichpic.href;
why not forget getElementById at this step (move it down a bit)
and use the images collection:
document.images['placeholder'].src = whichpic.href;
if (whichpic.title) {
document.getElementById('desc').childNodes[0].nodeValue =
whichpic.title;
} else {
document.getElementById('desc').childNodes[0].nodeValue =
whichpic.childNodes[0].nodeValue;
}
and do your getElementById thing here:
if (document.getElementById) {
var d = document.getElementById('desc').childNodes[0];
if (whichpic.title) {
d.nodeValue = whichpic.title;
} else {
d.nodeValue = whichpic.childNodes[0].nodeValue;
}
or more concisely:
if (document.getElementById) {
var d = document.getElementById('desc').childNodes[0];
d.nodeValue = whichpic.title ||
whichpic.childNodes[0].nodeValue;
}
It's easier on the eye, also the images collection is likely
more widely supported (just?) than getElementById, so if it
fails, at least there's still chance the image will display (but
without a title).
Choose whatever suits best.
return false;
} else {
return true;
}
}
There is no need for the 'else', if the script gets this far,
just return true unconditionally.
return false;
}
return true;
Full code (tested in IE and Firefox):
<a href="1.gif"
title="Logo"
onclick="
SwapImage('no1','','/images/1.gif','no2','',
'../../../images/common/2-up.gif',1);
var x = showPic(this);
document.location='#bottom';
return x;
"><img src="1.gif"
name="no2" width="27" height="33"
id="no2"></a>
<input type="button" onclick="alert(document.images['no2']);"
value="click">
<script type="text/javascript">
function SwapImage(){
// just a dummy
}
function showPic (whichpic) {
document.images['placeholder'].src = whichpic.href;
if (document.getElementById) {
var d = document.getElementById('desc').childNodes[0];
d.nodeValue = whichpic.title ||
whichpic.childNodes[0].nodeValue;
return false;
}
return true;
}
</script>
<div style="height: 2000px"> </div>
<img id="placeholder" name="placeholder" src="" alt="">
<!-- Must put something in 'desc' or script may fail in some
browsers 'cos childNodes[0] may not be created otherwise -->
<p id='desc'> </p>
<a name="bottom"></a>