Forum Moderators: open

Message Too Old, No Replies

IE "Object Expected" error, no errors in firefox

         

IdahoEv

10:02 pm on May 17, 2007 (gmt 0)

10+ Year Member



Thanks for looking. This page contains only a few lines of javascript, they work fine in firefox & safari, but have stopped working in IE. (They used to work in IE just fine, and I'm not sure what I changed.)

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]

littlegiant

10:41 pm on May 17, 2007 (gmt 0)

10+ Year Member



Did you fix this already because it seems to be working fine for me in both IE6 and IE7.

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...

rocknbil

7:53 am on May 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard IdahoEv, you'll find your URL's removed soon - have a look at the site TOS, no links. But I agree, it's working - as you expect.

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. :-)

littlegiant

2:04 pm on May 18, 2007 (gmt 0)

10+ Year Member



Hmmm yeah... I didn't think IdahoEv was concerned about accessibility issues since he was using # for the URL anyway. But yes, you're absolutely right, rocknbil.