Disable Left Click on Images in Blogger Posts
This is the default behaviour of posted images in Blogger, it's specially designed for image lightbox but if the lightbox is disabled images are opened in new tabs, it's because the images you add in post editor are automatically wrapped in an anchor element with a link to the original image which makes the image a link to the its source.
There's no settings in Blogger Dashboard to turn this function on or off so you have do some extra work to prevent images from opening in new tab if it's clicked. There are two ways to do that.
Editing HTML of each and every post
Open your blogger post editor, upload an image in a blank post and then switch to the HTML mode (use the buttons on top left). This is what you will see :
1
2
3
4
5
| <div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3LX25Gp2oOS0_L2i9ikydvRD3Y0pgYmLGmJY1IFFuOfEQ2KRdtSu3PmM1ibBLHXeiGMRj-DHlKdQvLRVhDyiqxLUz2bgUXxImroFT0y_CXbtWGYNOv9nozXQsAIdyJ0JvDoNu09imJH-j/s1600/301804_419266174773556_195336197166556_1276547_1117941733_n.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3LX25Gp2oOS0_L2i9ikydvRD3Y0pgYmLGmJY1IFFuOfEQ2KRdtSu3PmM1ibBLHXeiGMRj-DHlKdQvLRVhDyiqxLUz2bgUXxImroFT0y_CXbtWGYNOv9nozXQsAIdyJ0JvDoNu09imJH-j/s320/301804_419266174773556_195336197166556_1276547_1117941733_n.jpg" width="286" /></a></div> |
But you have to do this every single time you upload an image which is a difficult and if you have hundreds of posts with thousands of images then this is going to be a boring job for you.
he JavaScript way
Adding the jQuery Library first
1
| <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> |
<head> and paste the JavaScript/HTML immediately below it. It may then look like this
1
2
3
4
| <head><script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>............. |
The jQuery Function Code
1
2
3
4
5
6
7
8
9
| <script type='text/javascript'>$(document).ready(function(){$('.post-body a img:not(.clickEnabled)').click(function(e) { if (e.which === 1) { e.preventDefault(); }});});</script> |
- Go to your Blogger Dashboard
- Select the Template tab
- Click on Edit HTML and proceed
- Now use CTRL+F or CMD+F on Mac
- Search for
</head>in the template and then paste the jQuery/JavaScript code just above it and Save the template.
1
2
3
4
5
6
7
8
9
10
| <script type='text/javascript'>$(document).ready(function(){$('.post-body a img:not(.clickEnabled)').click(function(e) { if (e.which === 1) { e.preventDefault(); }});});</script></head> |
clickEnabled class to the img tag. So a click enabled image should look like this (just notice the class name)
1
| <img src='some-image.png' title='An amazing image' class='clickEnabled'/> |
Not only on blogspot, you can use this snippet of code on any website you want Wordpress, Tumblr etc. just change the selector (the
.post-body a img:not(.clickEnabled) thing) but if you aren't sure about it you can contact me to know the correct selector for you site or blog. This code simply stops the browser from doing anything when the link images inside post body are clicked, simple, isn't it ?
If you are facing problems implementing this code into your blog, do let us know in the comments, also remember to include your blog address with the comment. If you have any suggestions, comments are welcomed.