I am a freelancer and looking for work

My name is Steve Cross and this blog is a collection of some of the interesting things I've found that I felt the need to share. However this blog is also intended to demonstrate my talent as a web designer and developer. If you like what you see and would like a new site, changes to an existing one or for your site to earn more money for you, then head over to my site and check me out.

 
 
 

How to automatically add an image to pdf links

How to automatically add a pdf image using jQueryOne of the things I like to do as standard is to add a nice little pdf logo to any pdf links. This allows users to quickly see that there is something a little bit special on the page and attracts the eye to content. This code will automatically update any pdf links without any further intervention from you. It’s useful if you’re using a CMS with content you don’t control yet want to define the style nicely.

Using jQuery, I add a class to any links with the extension ‘pdf’ using the following code. This can be placed before the </head> tag on your page.

 

// add class of pdf to links that end in pdf and open in a new window

$(document).ready(function() {

$(“a[href$=pdf]“).addClass(“pdf”);

$(‘a[rel="external"]‘).click( function() {

window.open( $(this).attr(‘href’) );

return false;

});

});

Then in the css file for my site I add the following class assignment

.pdf {

background: url(../images/pdf-icon.gif) 100% 50% no-repeat;

padding-right: 28px;

}

We then receive the nice effect below. (You may right click the pdf image and use it for yourself)
PDF Link Download Report Here

  • by codemonkeysteve
  • April 20, 2010
 
-->
 

Have something to say?