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 11 ■ Using Third-Party <strong>JavaScript</strong><br />

You use toggle() to show <strong>and</strong> hide the code examples; however, unlike what you did <strong>with</strong> the last script, you<br />

store the original text of the link in a property called old when you show the code <strong>and</strong> replace the link text <strong>with</strong> “Hide<br />

Code”. You then invoke the function parseCode() <strong>with</strong> the link as a parameter when you show the code. When you<br />

hide the code, you restore the original link text by setting the link text back to the value stored in the old parameter<br />

before hiding the PRE element <strong>with</strong> jQuery’s hide() method.<br />

jqueryTest<strong>Ajax</strong>.js (continued)<br />

function parseCode(o){<br />

if(!o.nextSibling.hascode){<br />

$.get (o.href,<br />

function(code){<br />

This function tests whether the PRE element following the link (which is its next sibling) has a property called<br />

hascode, which you will set once the code has been loaded for the first time. This is necessary to avoid the script<br />

loading the code every time the user clicks the link—instead, loading it only once. You then use the $.get()method<br />

<strong>with</strong> the link’s href attribute value <strong>and</strong> an anonymous function as parameters. This effectively sends an request<br />

that loads the linked document <strong>and</strong> invokes the function once the document has been loaded. You send a parameter<br />

called code, which contains the content of the document that was loaded via XHR.<br />

jqueryTest<strong>Ajax</strong>.js (continued)<br />

code=code.replace( /&/mg, '&#38;' );<br />

code=code.replace( //mg, '&#62;' );<br />

code=code.replace( /\"/mg, '&#34;' );<br />

code=code.replace( /\r?\n/g, '' );<br />

code=code.replace( //g, '' );<br />

code=code.replace( / /g, '&#160;' );<br />

o.nextSibling.innerHTML = ''+code+'';<br />

o.nextSibling.hascode = true;<br />

You then use regular expressions to replace all ampers<strong>and</strong>s, tag brackets, <strong>and</strong> quotation marks <strong>with</strong> their<br />

numbered HTML entities, line breaks <strong>with</strong> , <strong>and</strong> spaces <strong>with</strong> their HTML entity before adding the result inside a<br />

CODE element as the innerHTML property of the PRE element. You set the hascode property to true to ensure that the<br />

next time the user clicks the link to show the code, the $.get() construct is skipped.<br />

jqueryTest<strong>Ajax</strong>.js (continued)<br />

}<br />

);<br />

}<br />

$(o.nextSibling).show();<br />

}<br />

}<br />

)<br />

All that is left to do is show the PRE element using jQuery’s show() method. Notice that you need to do that<br />

outside $.get() the construct to make sure the code gets shown the second <strong>and</strong> subsequent times the user chooses to<br />

show the code.<br />

328<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!