HackingUniversity - Hacks . Tricks . How-To's

05 October 2013

Automatically Link Blog Post to Specific Keyword in Blogger

blogger-logoSometimes you want link a specific keyword in your blogger posts to a perfect targeted post that might get some awesome exposure and side by side provide some useful knowledge to all your readers but editing every single post is a nightmare and you do not have much time and stamina to do that if you have a whooping 1000+ posts on your blog. Well but if that thing gets automatic using javascript it would be a life saver, isn't it.

Well I found a useful script like I have earlier posted few awesome one's for redirecting your 404 links to homepage and even removing all links from your blogger comments. Well this script allows you to mark a specific keyword and even link it up with any post from your blog. So you have a free option to choose that keyword and even choose the post you want to link it too.

Example :) So now to explain you how this script works just take an example you have a keyword "SEO" in many of your blog posts and you want that keyword to link to some post like "www.hackinguniversity.in/2013/01/seo.html" or anything so this could be easily done using this script. So now lets get started.

  1. Open Blogger > Template > Edit HTML.
  2. Press Ctrl + F & search for </body> tag & paste below code above it.

    <script src='//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'/>

     

    <script type="text/javascript">
    function autoLink(){
        this.keywdHref = new Object();
        this.add = function(keyword, href){
            if(keyword.substr(0,1) != &quot; &quot;){keyword = &quot; &quot; + keyword;}
            this.keywdHref[keyword] =  href;
        }
        this.createAnchor = function(){
            var objs = document.getElementsByTagName(&quot;div&quot;);
            for(var i=0; i&lt;objs.length; i++){
                var obj = objs[i];
                if(obj.className.indexOf(&quot;post-body&quot;)&gt;-1){
                    var content = obj.innerHTML;
                    for(var keyword in this.keywdHref){
                        var href = this.keywdHref[keyword];
                        var newstr = content.replace(keyword, &quot;&lt;a href=&#39;&quot;+href+&quot;&#39;&gt;&quot;+keyword+&quot;&lt;/a&gt;&quot;, &quot;gi&quot;);
                        obj.innerHTML = newstr;
                        content = newstr;
                    }
                }
            }
        }
        this.startScript = function(){
            var onLoad = window.onload;
            window.onload = function(){
                if(onLoad){onLoad();}
                setTimeout(&quot;f.createAnchor()&quot;, 100);
            }
        }
    }
    </script>

     

    <script type="text/javascript">
    var f = new autoLink();
    f.add(&quot;KEYWORD&quot;, &quot;http://www.hackinguniversity.in/&quot;);
    f.startScript();
    </script>

  3. Now just change the text in blue with your Keyword.
  4. And change the red text with your blog post URL.
  5. Now after making all these changes just press save template & refresh your blog.
  6. So now just check any of your post with that Keyword and see how that keyword will be linked up to that post you have mentioned above.

Remember This script only allows you to link one keyword, make sure to have the same case as your keyword is to get it linked up.

So that ends this another awesome tutorial briefing you how to easily link a specific keyword in your blogger posts automatically using javascript. So just enjoy this script and do comment if you face any problem using this script.