Forum Moderators: open
This is all from <url removed>
This script is in the header:
<script type="text/javascript">
<!--
/* <![CDATA[*/
function imgswap(imgname,imgcaption,imgcredit,imgurl)
{
document.getElementById('mainimage').src='Includes/images/db_medium/'+imgname;
document.getElementById('mainimagelink').href='Includes/images/db_full/'+imgname;
document.getElementById('maincaption').innerHTML=imgcaption;
if(imgurl!=null)
document.getElementById('maincredit').innerHTML='<a href="'+imgurl+'">'+imgcredit+'</a>';
else
document.getElementById('maincredit').innerHTML=imgcredit;
}
-->
/* ]]> */
</script>
And there are several onclick() calls attached to thumbnails scattered around the page that look like this:
<a href="#" onclick="imgswap('Claremont_01A.jpg','','Douglas Olson Photography','http://www.douglasolson.com/'); ">
clicking on any thumbnail generates an "Object Expected" error. The line numbers reported appear to be the line with the call to imgswap(); There's no other javascript on the page, either included or linked in.
Thanks for any input!
[edited by: encyclo at 12:38 pm (utc) on May 18, 2007]
[edit reason] no URLs please, see TOS #13 [webmasterworld.com] [/edit]
Also, wouldn't it be better to simply set the URL as javascript:imgswap(...etc...); so that the page wouldn't jump up when you click on the thumbnails to swap the images? Then you could do away with the onclick event handler altogether. Just a suggestion...
wouldn't it be better to simply set the URL as javascript:imgswap(...etc...);
Actually you really *don't* want to do that because then the link is completely javascript -dependent. (The jumping issue can be addressed as below.) What happens if Javascript is disabled? What implications does href="javascript..." have on a search engine's ability to follow links on your page?
href="#" is not a great idea either, causing that unsettling jump back to top. When this happens, the user is scrambling along the top of the page, expecting they were brought here for a reason, and won't even see your image swap. What **should** happen if Javascript is disabled is the link should go to the swapped image which is what the user will expect. But you can still eliminate the onClick event from the link. Begin by creating a function for the href object, identified by ID. Added to your JS:
function attachClickEvents () {
if (document.getElementById) {
document.getElementById('claremont_link').onclick = function() { imgswap('Claremont_01A.jpg','','Douglas example','http://www.example.com/'); };
}
}
window.onload=attachClickEvents();
Then you have
<a id="claremont_link" href="Claremont_01A.jpg">
By substituting the image for #, we've eliminated the top-of-page jump, but you might ask, won't the main window page GO to Clairemont_01A.jpg when the link is clicked? It won't if you add return false at the end of the imgswap function:
function imgswap(imgname,imgcaption,imgcredit,imgurl) {
.....
return false;
}
If javascript is functioning, the false return from the function halts the normal behavior of the object, whether it's a link or a form, anything. So if JS is enabled, it will run the imgswap function and when it returns false will tell the browser not to move to the page or image in the link; if Javascript is disabled, it will go to the image, allowing non-JS users to access your content. And it feeds S.E.'s an actual link to follow. All without bunging up the simple link. :-)