Adding Pinterest button to a lightbox

Monday October 22, 2012 ()

In this demonstration we will use slimbox 2, a lightbox 2 clone. Slimbox, like lightbox, and most its clones, uses the title attribute of a link to render a caption near the bottom of the frame, below the image. It is this title that we also use to add our Pinterest button. Using a link as title directly will not work, aside from the resulting title, which become the tool tip of the link, is ugly.

Slimbox is an image only lightbox.

Standard lightbox

This is how Slimbox is used.

<a href="" rel="lightbox" 
	title="Barilla was the first coin struck in the country.">
	<img src=""
		width="180" border="0" alt="How to add pinterest to a lightbox" />

This an image link that will display our lightbox. For this code to work, jQuery and the Slimbox plugin must be in the page. You add them to the head section of the page similar to the following:

<!-- jQuery -->
<script type="text/javascript"

<!-- Slimbox -->
<script type="text/javascript" src="/kauswagan/slimbox-2.04/js/slimbox2.js">
<link rel="stylesheet" 
	type="text/css" media="screen" />

The slimbox lines above may be slightly different depending on the location of your slimbox plugin. Download your plugin here if you don't have them already.


Adding Pinterest to the above lightbox requires the synchronous version of Pinterest script. Below is a snippet that employs the synchronous Pinterest script to draw the button.

	<a href="
		description=Barilla was the first coin struck in the country."
	<script type="text/javascript" src="" />

Please visit the Pinterest website if you need more explanation of the fields above.

Adding the Pinterest button to a lightbox

To add a Pinterest button, do not code directly the button into the title attribute. As already mentioned, it will not work. Add the title, which is our Pinterest button (or any any other link/anchor), by handling the onmousedown of the link. In our example below, we also use onmouseover to prevent the link (the title) from displaying as tooltip while you hover over the link and while the lightbox is not yet displayed.

<a href=""
		<img src=""
		width="180" border="0" alt="How to add pinterest to a lightbox" />

And below is the javascript onmousedown handler.

<script type="text/javascript">
	function addPinterest(lightbox) {
		lightbox.title='<a href="';
		lightbox.title +='url=';
		lightbox.title +='media=';
		lightbox.title +='description=Barilla, first coin struck in the country." ';
		lightbox.title +='class="pin-it-button" ';
		lightbox.title +='count-layout="horizontal"></a> '
		lightbox.title += '<script type="text/javascript" '
		lightbox.title +='src=">';

Add the Javascript code above in the head section of your page and it is all set. Here is a demo. You may also want to view the source of this demo page.

Note that depending on the speed of your connection, there may be a very slight delay before Pinterest button renders itself (demo).

That's it Good Luck.


Comments (Adding Pinterest button to a lightbox )