So, once upon a time, a really long while ago, I tried to customise and recreate a Blogger template which is not available anymore, but there's one thing I remember about it. I had a jQuery code written to make all the external links open in new tab. I also thought, it's about time I revive that idea and write a more intact code and here we go.
So, there are two things to do.
- Add Javascript code that detects external links and opens them in new tab.
- Add a CSS code to let users know that a particular link is going to be opened in new tab.
Adding a javascript code
Please follow the instructions below,
- Go to https://blogger.com dashboard
- Navigate to Themes > click the down arrow near "Customize" and then click on Edit HTML.
- Now scroll to the very end and paste the following code just before </body> tag.<script type='text/javascript'>document.body.addEventListener("mousedown",function(event){if(event.target.tagName === "A"){if(!event.target.href.includes("krishnainfotron") && event.target.href.startsWith("http")){event.target.setAttribute("target","_blank");}}},true);</script>
- The code above should look like,
- Here, please replace krishnainfotron with the keyword that your blogspot and custom domain have in common. For example, my blogspot address is https://krishnainfotron.blogspot.com and my custom domain address is https://howtoblog.krishnainfotron.com. Since both of them have krishnainfotron in common, I have added that as keyword so that both blogspot and custom domain links in my blog will be detected as own link and open others in a new tab.
- Once done, please click on the floppy icon at the top right which indicates Save.
Now, your blog will open all external links in new tab. Please test it out. Let's move on to the second part of the article,
Adding a CSS code
This CSS code is to show an indication that it's an external link and will open link in new tab. Please follow the instructions below,
- Go to https://blogger.com > Themes > Customize
- In the left, click on Advanced > In the appearing dropdown, choose "Add CSS"
- In the appearing text box, please paste the following code,a[href]:not([href*="krishnainfotron"]):not([href*="bp.blogspot"]):not([href*="blogger.com"]):not([href*="googleusercontent"]):not([href=""]):not([href^="/"]):not([href*="javascript"])::after {content: "";background-image: url('https://static.thenounproject.com/png/299638-200.png');background-size: 15px;display: inline-block;width: 15px;height: 15px;margin-left: 5px;margin-right: 5px;bottom: 0px;}
- Here, again please change your blog's keyword. The rest of the restrictions are to ignore image links, blogger original links etc., Although they will be opening in new tab, we don't indicate them for neatness.
- Click the floppy icon in the bottom right that represents save.
- Now you can see external links in my blog having a "Open in new tab" icon nearby as shown below,
Being all that done, it should be sufficient for opening external links in new tab at lowest code cost. Happy blogging everyone.
Please note that the image is taken from thenounproject.com with creative commons licence and it is best to give attribution for using it like I have given in my right side bar using a HTML/Javascript gadget,
(P.S - If at all do you face any problems like some links that are not supposed to open in new tab opens etc., please comment on this article so that I can check and rectify it)
Nice 🙂
ReplyDeletehelpful but... it's opening all the links in new tab now. I mean even the links which leads to a blogpost of the same blog. It's a problem. please reply me how can I solve it. Thank you
ReplyDeleteIt might be too late that you figured out the issue but am happy to help if you could create new thread at the blogger help forum -> https://support.google.com/blogger/thread/new
Delete