14.08.2016 Views

Beginning JavaScript with DOM Scripting and Ajax, 2nd Edition

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

var p = document.getElementById('back');<br />

if( p ) {<br />

var newa = document.createElement('a');<br />

newa.setAttribute('href', '#');<br />

newa.appendChild( document.createTextNode('back to previous page') );<br />

newa.onclick = function() { window.back();return false; }<br />

p.replaceChild( newa, p.firstChild );<br />

}<br />

}<br />

window.onload = backlink;<br />

■ ■Caution The danger of these methods is that you offer functionality that the browser already provides the user. The<br />

difference is that the browser does it better, because it supports more input devices. In Firefox on a PC, for example, you<br />

can print the document by pressing Ctrl+P, close the window or tab by pressing Ctrl+W, <strong>and</strong> move forward <strong>and</strong> backward<br />

in the browser history via Alt <strong>and</strong> the left or right arrow keys.<br />

Even worse, the functionality offered <strong>with</strong> these methods is dependent on scripting support. It is up to you to decide<br />

if the preceding method—creating the links invoking these methods, which is probably the cleanest way to deal <strong>with</strong><br />

the issue—is worth the effort, or if you should just allow the user to decide how to trigger browser functionality.<br />

Generating links also has the effect of changing the browser history. In this case, you are not navigating to pages that<br />

are in the browser history, you are adding new pages to the history.<br />

Alternatives to Opening New Windows: Layer Ads<br />

Sometimes there is no way to avoid pop-up windows, because the site design or functionality requires them, <strong>and</strong> you<br />

just cannot make them work because of the browser issues <strong>and</strong> options explained earlier. One workaround is layer<br />

ads, which are basically absolutely positioned page elements put on top of the main content.<br />

Let’s try an example of those. Imagine your company wants to advertise its latest offers quite obviously when the<br />

page is loaded. The easiest way to do this is to add the information at the end of the document <strong>and</strong> use a script to turn<br />

it into a layer ad. When you use this approach, visitors <strong>with</strong>out <strong>JavaScript</strong> will still get the information, but they won’t<br />

get anything that covers the content <strong>with</strong>out giving them a chance to get rid of it. The HTML can be a simple DIV <strong>with</strong><br />

an ID (the real links have been replaced <strong>with</strong> “#” for the sake of brevity):<br />

exampleLayerAd.html (excerpt)<br />

<br />

We've got some special offers!<br />

<br />

TDK DVD-R 8x 50 pack $12<br />

Datawrite DVD-R 16x 100 pack $50<br />

NEC 3500A DVD-RW 16x $30<br />

<br />

<br />

190<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!