{"authorId":1,"title":{"title":{}},"slug":"make-your-content-smarter-with-jquery","postDate":1221095144,"expiryDate":null,"enabled":1,"fields":{"2":"<p>At work today, we were asked by a client to add a link to download Adobe Acrobat onto every page that had a PDF linked on it. Since the site was mostly static, with some content coming from the content management system, we were not interested in adding it to all of the static pages and changing the programming of the CMS output pages.<\/p>\n<!--more-->\n<p><a href=\"http:\/\/seeyouin1984.com\/\">My co-worker<\/a> and I decided that the best way to do it would be with JavaScript. Since the site was already using jQuery, our company&rsquo;s library of choice, it turned out to be quite a simple task. If you just want to get to the code, <a href=\"http:\/\/trevordavis.net\/play\/jquery-pdf-links\/\">take a look at the demo<\/a>.<\/p>\n<h2>The Idea<\/h2>\n<ol>\n<li>We add the link to download Adobe Acrobat to the site&rsquo;s template<\/li>\n<li>We hide the link using CSS<\/li>\n<li>We use jQuery to see if there are any links on the page linking to a PDF<\/li>\n<li>If there are, we use jQuery to show the link<\/li>\n<\/ol>\n<h2>The Execution<\/h2>\n<p>Ok, simple enough, just add the link to the source of the document:<\/p>\n<div class=\"codeblock\"><code>\n&lt;p&nbsp;id=\"acrobatDownload\"&gt;&lt;a&nbsp;href=\"http:\/\/www.adobe.com\/products\/acrobat\/readstep2.html\"&gt;Download&nbsp;Adobe&nbsp;Acrobat&nbsp;to&nbsp;view&nbsp;the&nbsp;PDFs&nbsp;on&nbsp;this&nbsp;page&lt;\/a&gt;&lt;\/p&gt;&nbsp;\n\n<\/code><\/div>\n\n<p>Then, hide it with CSS:<\/p>\n<div class=\"codeblock\"><code>\n#acrobatDownload&nbsp;&#123;&nbsp;display:&nbsp;none;&nbsp;&#125;&nbsp;\n\n<\/code><\/div>\n\n<h3>The jQuery<\/h3>\n<p>First, we want to check and see if any of the links on the page end with <strong>.pdf<\/strong>. Then, if there are any, we fade in the link:<\/p>\n<div class=\"codeblock\"><code>\n$(document).ready(function()&nbsp;&#123;<br \/>&nbsp;if($('a&#91;href$=.pdf&#93;').length&nbsp;&gt;&nbsp;0)&nbsp;&#123;<br \/>&nbsp;&nbsp;$('#acrobatDownload').fadeIn('slow');<br \/>&nbsp;&#125;<br \/>&#125;);&nbsp;\n\n<\/code><\/div>\n<p>I have created <a href=\"http:\/\/trevordavis.net\/play\/jquery-pdf-links\/\">a demo page with a PDF link on it<\/a> to see this in action.<\/p>\n\n<h2>Conclusion<\/h2>\n<p>I can&rsquo;t even imagine how much time and hassle we saved ourselves by doing this with JavaScript. Plus, it could provide useful to someone who doesn&rsquo;t have Acrobat (although I can&rsquo;t imagine that there are many).<\/p>\n\n<p>The concept is actually quite simple, and my mind is grinding away trying to think of other ways to use jQuery to do things like this.<\/p>\n\n<p>Can you all think of anything?<\/p>","4":{"website":{}}}}