14.08.2016 Views

Beginning JavaScript with DOM Scripting and Ajax, 2nd Edition

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 6 ■ Common Uses of <strong>JavaScript</strong>: Images <strong>and</strong> Windows<br />

picturePopup.js (continued)<br />

killPopup : function( e ) {<br />

pop.popContainer.parentNode.removeChild( pop.popContainer );<br />

pop.popContainer = null;<br />

<strong>DOM</strong>help.cancelClick( e );<br />

}<br />

}<br />

<strong>DOM</strong>help.addEvent( window, 'load', pop.init, false );<br />

The result is that you can click any image pointing to a photo <strong>with</strong> the correct class, <strong>and</strong> it’ll show the image<br />

below it. Figure 6-17 shows an example.<br />

Figure 6-17. An example of a dynamically displayed photo<br />

The beauty of this approach is that it is not only limited to photos. With just a simple modification, you can display<br />

other documents inside the current one. The trick is to dynamically add an IFRAME element to the photoContainer<br />

<strong>and</strong> set its src attribute to the document you want to embed. The demonstration exampleIframeForPopup.html does<br />

exactly that to show a lengthy terms-<strong>and</strong>-conditions document inside the main one.<br />

The only differences (apart from different property names, because the method does not show a photo) are in the<br />

openPopup method, where you add the new IFRAME:<br />

iframeForPopup.js (excerpt)<br />

var ifr = document.createElement('iframe');<br />

pop.ifrContainer.appendChild( ifr );<br />

ifr.setAttribute('src', t.getAttribute('href') );<br />

Figure 6-18 shows what this could look like.<br />

www.it-ebooks.info<br />

195

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!