First I'd like to explain why I added the if statement, and also mention that I did so incorrectly due to a typo that I have since fixed. With it, there will be no error, as well as no action taken, if the element with the id isn't there. This is preferable because lightbox.js may be linked to any number of pages, not all of which may have this element on it.
The explanation of:
Code:
firstLight.onclick.apply(firstLight);
is that firstLight (if the link with the id exists and is a lightbox link) represents a lightbox link. Lightbox assigned all rel="lightbox" links the following onclick event:
Code:
myLightbox.start(this); return false;
Calling firstLight.onclick runs the event as an independent function. If we use the apply method, the function's 'this' keyword will apply to the item listed. So it will be as if we clicked on it.
We should go a step further in testing and do:
Code:
function initLightbox() { myLightbox = new Lightbox();
var firstLight;
if((firstLight=document.getElementById('id')) && /^lightbox/.test(firstLight.rel))
firstLight.onclick.apply(firstLight);
}
Event.observe(window, 'load', initLightbox, false);
That will ensure that the link with the id with the rel including 'lightbox' exists. Let's break it down:
just establishes the variable.
Code:
if((firstLight=document.getElementById('id')) && /^lightbox/.test(firstLight.rel))
is a compound test and assignment that could be written more clearly like so:
Code:
if(document.getElementById('id')){
firstLight=document.getElementById('id');
if(/^lightbox/.test(firstLight.rel))
then do whatever
}
It sees if the element is available, if so assigns it to the variable, then asks if the element has 'lightbox' at the start of its rel attribute. If all that's in order, then:
Code:
firstLight.onclick.apply(firstLight);
which I already covered.
Bookmarks