Stop all Internal Links From Opening In New Window


Open links in new window in blogger and wordpressIn 2013 we shared a script snippet that helped you to automatically open all external links in a new browser window. That script received great feedback and we thought to further enhance it. Today's jquery script will prevent internal links containing the attribute target="_blank" from opening in a new tab. It will open all links inside a new window except internal links. This plugin will look for all http and https hyperlinks inside your blog content section which includes the blog post body and comment body. It will then add a target="_blank" attribute to all external links but will remove target="_blank" attribute from all internal links found inside your blog post and comments.

The target attribute inside <a> tag was never meant for internal links at all and therefore it makes no sense to irritate your website visitors by opening blog links inside new tabs thus forcing them to close the previous page in order to read the current page in new tab. The tutorial below includes instructions for both blogspot blogs and wordpress blogs. This script will immensely improve your daily pageviews and will tremendously decrease the overall blog bounce rate thus leading to a better search engine ranking and of course blog revenue.  We have implemented it on mbt and also on our wordpress blog and it is working just great Alhamdulillah.

Open all links In New Tab Except Internal Links

Blogger templates and Wordpress themes have different DOM structures and therefore due to the difference in class and ID name, I will be discussing the installation steps separately for both platforms.

FOR BLOGGER

1 Go To Blogger > Template

2 Backup your template

3 Click Edit HTML

4 Paste the following script just above </head> tag.

<script async='true' src='https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js' type='text/javascript'></script>

<script type='text/javascript'>
//<![CDATA[
//Open External Links in New Window - Plugin by STCnetwork.org
$(document).ready(function(){
$("#Blog1 a[href^=http], #Blog1 a[href^=https]")
  .each(function(){
    if (this.href.indexOf(location.hostname) == -1){
      $(this).attr({ "target":"_blank" })}
    if (this.href.indexOf(location.hostname) != -1){
    if ( $(this).attr('target') == '_blank')  {$(this).attr("target", "");}}
  });});

//]]>
</script>

Note: Remove the yellow highlighted code if you already have added jQuery library inside your template

5 Save your template and all done!

FOR WORDPRESS

Follow these steps for a wordpress blog to open external links in a new window but open internal links inside the same window.

1 Go to your theme settings page or go to Appearance > Editor > header.php

2 Paste the following code just above </head> tag.

<script async='true' src='https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js' type='text/javascript'></script>

<script type='text/javascript'>
//<![CDATA[
//Open External Links in New Window - Plugin by STCnetwork.org
$(document).ready(function(){
$("#content a[href^=http], #content a[href^=https]")
.each(function(){
if (this.href.indexOf(location.hostname) == -1){
$(this).attr({ "target":"_blank" })}
if (this.href.indexOf(location.hostname) != -1){
if ( $(this).attr('target') == '_blank') {$(this).attr("target", "");}}
});});

//]]>
</script>

Note: Remove the yellow highlighted code if you already have added jQuery library inside your template

3 Save  Settings and you are all done!

How are Internal and external links Identified?

The script looks for all links inside the post body and comment body. It then removes target attribute from all internal links and adds it in external links only. The location.hostname variable checks if the link is an internal link or external. For external links the condition is set to ==-1 and for internal links the condition is set to !=-1

If you are using my old script, I will recommend that you replace it with this new script which is much more lightweight and SEO friendly..

I hope these new script may put a positive effect on your overall page impressions and help you engage readers more on your site instead of irritating them with popping links in new tabs. Let me know if you need any help. =)

Need Quick Help within 24 Hours? ASK NOW



If you don't want to get yourself into Serious Technical Trouble while editing your Blog Template then just sit back and relax and let us do the Job for you at a fairly reasonable cost. Submit your order details by Clicking Here »

27 comments : Post Yours! Read Comment Policy ▼
PLEASE NOTE:
We have Zero Tolerance to Spam. Chessy Comments and Comments with 'Links' will be deleted immediately upon our review.

  1. kindly tell me any wordpress plugin that changes the tags into meta keywords???

    ReplyDelete
    Replies
    1. meta keywords are no more supported by search engines and it is dead now. SE really dont consider them, they are efficient enough now to judge keywords for your content after crawling it.

      Delete
  2. Not working in blogger please check.

    ReplyDelete
    Replies
    1. did you sincerely tried twice? =)

      This is the simplest work. Make sure you have not added jquery library twice

      Delete
    2. Yes I have tried may times. jquery library is added one time only, please try again and revert.

      Delete
    3. I checked your blog, you have added the wordpress code inside your blogger template. Kindly add the blogger code only.


      PS: jQuery library was added thrice on your blog.

      Delete
  3. Respected Mohammad Mustafa Brother, if we don't stop internal links in a new window and all internal and external links open in a new window. So is it better for SEO and bounce rate?

    ReplyDelete
    Replies
    1. It's like a cup-cake for SEO. Extremely useful and recommended.

      Delete
  4. Hi Brother Mohammad Mustafa!
    First of all thanks for this helpful article its really amazing and thanks for your effort and guide for pre blogger to lead toward success.

    bro i have a problem i registered domain name and hosting and when i type url this show 403 error
    please help
    a4abidaziz blogspot com to
    onlinebloggertricks com please reply

    ReplyDelete
    Replies
    1. buddy DNS propagation takes around 24 hours to properly redirect your name servers. Your blog is working fine now. I just checked it. =)

      Delete
  5. Hello Brother! I Always Love To Your Articles :D
    One Thing I Wanna Told You That Just Started A Blog Check That And Tell Me I'm Going In Right Or Wrong Direction.
    Your Reply Is Much Appreciated Brother/Sir!
    My Blog Link Is : Blogging Earning
    I Hope I Will Get Instant Reply From You :D
    You Always Make Me Smile By Your Awesome Article Dear!
    Hats Off To You! :D :D :D

    ReplyDelete
    Replies
    1. I am always happy to see work of enthusiastic new comers like you. Well done, your start is really good. Just focus on unique and quality content. Avoid copyrights violation always and focus seriously on latest SEO trends.

      My best wishes are always with you. All the best =)

      Delete
    2. Thanks For Your Awesome Wishes Brother .. Means Alot To Me .. :D
      Actually I Wanna Write a Guest Post For MBT!! Can I?
      Your Reply Means Alot To Me Sir!

      Delete
    3. I Didn't Get Your Answer .. Please Answer Me My Question!

      Delete
    4. Hi Abdul,

      We allow guest posts to readers who have followed us for over two years and participated actively on our forum and blog. I will surely give you a chance once you spend some time exploring the blog and helping our community.

      Delete
  6. Awesome script there brother. Life savor <3 Thank you for for posting that :)

    ReplyDelete
    Replies
    1. Thank for the kind feedback always Hassam
      As long as this feedback is alive , blogging will thrive :)

      Delete
  7. Hello Mohammad, am a follower of your blog from Nigeria and your blog has being an inspiration for my blog.
    You can have a quick view of it www.mightyfada.com and please I need your help concerning google adsense. Have applied and yet they keep saying my application didnt meet their requirements. Please help view my blog and see if you could find out why.

    ReplyDelete
    Replies
    1. Thank you buddy for the kind feedback. I would love to help but kindly ask Off-topic questions on our public forum. The community will surely help within 24 hours or I will answer myself inshAllah. Do post your question in detail there. =)

      Delete
  8. If i use plug in for word press for external link , so this will be or not...

    ReplyDelete
  9. Thank you Mustafa Bhai for sharing this helpful script :)

    ReplyDelete
  10. This was exactly the script I was looking for Mohammad. Thank you so much. However, for some reason I can't get it to work. My site is masteringpersiancooking.com. What am I doing wrong? Your help is greatly appreciated!

    ReplyDelete
  11. Thank you so much Mohamed, another great post from you, this is what exactly i was searching for and thanks God i saw your post, sadly i tried the code and it doesn't work, internal links still open in new tab, so please can you check my blog and help to fix this problem ? http://usefulweightlossideas.blogspot.com/

    ReplyDelete
  12. Wordpress script not working bro please fix it

    ReplyDelete