faqts : Computers : Programming : Languages : JavaScript : Images

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

58 of 72 people (81%) answered Yes
Recently 5 of 10 people (50%) answered Yes

Entry

How can I change an IMG that is in a(nother) layer?
How can I change an IMG that is in a(nother) layer?

Mar 6th, 2000 00:00
Martin Honnen, Shiny Leung,


First of all, with IE4+ and NN6 IMG elements are simply referenced with
  document.imageName
whether they are in a layer/positioned element or not. So your image 
swapping code doesn't change you still set
  document.imageName.src = 'whatever.gif'
or
  document['imageName'].src = 'whatever.gif';
NN4 gives each layer/positioned element its own document (much like a 
window or frame) so for NN4 you reference first the layer
  window.document.layerName
then its document
  window.document.layerName.document
and then the image in the layer document
  window.document.layerName.document.imageName
so you set
  if (document.layers) 
    window.document.layerName.document.imageName.src = 
      'whatever.gif';
  else
    document.imageName.src = 'whatever.gif';
to swap an image in cross browser code.
As for all layer stuff keep in mind that with NN4 nested layers produce 
nested layer references so for two nested layers you get
  var outerLayer = window.document.outerLayerName;
  var innerLayer = outerLayer.document.innerLayer;
  var img = innerLayer.document.imageName;
  img.src = 'whatever.gif';