[Javascript] How to separate the href + V8 moment

Scott Moore moore.scott at gmail.com
Thu Aug 11 11:02:02 CDT 2005


Thanks to everyone for their comments and suggestions. Here's where  
I'm at now, I've gone back to the original script and added Triche's  
code for denoting the first selected.

var Ary=new Array();
var firstSelected;

window.onload = function() {
firstSelected = document.getElementById('first');
firstSelected.className = "selected";
}

function showPic (whichpic) {
     if (firstSelected.className == "selected"){
     firstSelected.className = "notselected";
     }

     if (document.getElementById) {
         document.getElementById('placeholder').src = whichpic.href;
         return false;
         } else {
         return true;
     }
}

All works as planned when called using:
<a onclick="return(showPic(this))" href="imgs/series/series01.jpg"  
id="first">
<a onclick="return(showPic(this))" href="imgs/series/series02.jpg"  
id="first">

So the next step would be how to figure out how to incorporate the  
ability to give each thumbnail class=selected when clicked and switch  
the current thumbnail with class=selected to class=notselected. The  
code that was handling this was

  if (!obj.set){
      obj.set=true;
      Ary[Ary.length]=obj;
     }

     for (i=0;i<Ary.length;i++){
      Ary[i].className='notselected';
     }

If that eventually works, then I think all the issues are solved and  
I'll provide the final code as I think it's a solid solution.


On Aug 11, 2005, at 10:51 AM, Triche Osborne wrote:

Scott Moore wrote:

> Triche, thanks so much for your help.
> The reason I wanted to pull the image name out of the onclick   
> function is so that those who have JavaScript turned off would  
> still  be able to view the large version of the image.
> I thought it might be as easy as changing
> document.getElementById('placeholder').src = whichpic;
> to
> document.getElementById('placeholder').src = whichpic.href;
>
As you've seen, the problem isn't that this doesn't work from a JS  
perspective, it's that the XHTML behavior negates it.


> However, that's not the case and the image href acts like a normal  
> link.
> Any suggestions?
>
The simplest solution I can think of is to use JavaScript to write  
out your anchors and images, then use NOSCRIPT tags to enclose the  
alternative content. If you have a lot of thumbnails you can use a  
loop structure to write them out. (If you're unsure how to do this,  
let me know and I'll help.)

Also, as I was munching my breakfast, I had a "V8 moment" and  
realized that I'd made the "selected" stuff more verbose than  
necessary. You still need to create the ID for the first anchor, but  
the script can be shortened to this:

var Ary=new Array();
var firstSelected;

window.onload = function() {
firstSelected = document.getElementById('first');
firstSelected.className = "selected";
Ary[0] = firstSelected;
}

function showPic (obj,whichpic) {

     if (!obj.set){
      obj.set=true;
      Ary[Ary.length]=obj;
     }

     for (i=0;i<Ary.length;i++){
      Ary[i].className='notselected';
     }

     obj.className='selected';
         if (document.getElementById) {
                 document.getElementById('placeholder').src = whichpic;
         }

}

This version initializes the checking array with the first object  
(anchor) which eliminates the first if().

Triche


_______________________________________________
Javascript mailing list
Javascript at LaTech.edu
https://lists.LaTech.edu/mailman/listinfo/javascript





More information about the Javascript mailing list