Display "Post Titles" using Blogger JSON Feed

blogger recent posts widgetIn Part#4 we discussed how to extract "Labels List" data from Blogger JSON feed API and print this list with hyperlinks. Today we will extract Post Titles from BlogSpot JSON feed by parsing the data in JavaScript, in other words we are creating the "Recent Posts widget" that will only display titles. Exact same approach and technique will be used. We are trying to make sure we discuss data extraction in simple steps through various tutorials before you could actually build a useful widget like a "recent posts widget" that could display Titles, Labels, Comment count, Thumbnail image and timestamp. Lets get straight to work!

Note: Click the Button below to see full list of topics under discussion.

Topics List

Where is "Title String" and "Title URL" stored?

The Post Title String is stored inside the [ ] entry array followed by { } title object node as shown in the screenshot below.

title string is stored here


To extract the Title string we will follow this path:

json.feed.entry[i].title.$t

Which means:

  • First go to json object
  • then feed object
  • then entry array. For array it is important to mention which element ID are we looking for that's why we expressed it as entry[i].
  • then finally extract the data from the branch $t inside title object

To extract the Title Link or URL we will follow this path:

json.feed.entry[i].link[j].href

Title link stored here

As you can observe the title href attribute is located inside the array [ ] link and on its 4th ID          i.e.{ } 4.

Since it is not important that href value might exist inside the fourth array element always therefore we will make sure to extract href value of only that node which contains the rel: "alternate" name/value pair.  

Since [ ] link node is an array itself that is why we expressed it as link[j] so to make sure which element ID of link are we accessing.

I hope you now know where each data exists. Lets write the script now to display recent post titles.

Retrieving Recent Posts Titles

Following is the complete JavaScript code that will extract recent Post titles from the JSON feed and print the list:

<!-- ######### Writing Callback Function ############# -->

<script type="text/javascript">

//----------------------------Defaults

var ListBlogLink = window.location.hostname;
var ListCount = 5;
var TitleCount = 70;

//----------------------------Function Start
function mbtlist(json) {
document.write('<ul class="mbtlist">');
for (var i = 0; i < ListCount; i++)
{
  
//-----------------------------Variables Declared

var listing= ListUrl = ListTitle =  "";

//----------------------------- Title URL
for (var j = 0; j < json.feed.entry[i].link.length; j++) {
      if (json.feed.entry[i].link[j].rel == 'alternate') {
        break;
      }
    }
ListUrl= "'" + json.feed.entry[i].link[j].href + "'";

//----------------------------------- Title Stirng
if (json.feed.entry[i].title!= null)
{
ListTitle= json.feed.entry[i].title.$t.substr(0, TitleCount);
}

//----------------------------------- Printing List

var listing = "<li><a class='mbttitle' href="
+ListUrl+
"target='_blank'>"
+ListTitle+
"</a></li>";
document.write(listing);
}
document.write("</ul>");
}
</script>

<!-- ######### Invoking the Callback Function ############# -->

<script>
ListBlogLink = "http://www.mybloggertricks.com";
ListCount = 8;
TitleCount = 70;

document.write("<script src='"+ListBlogLink+"/feeds/posts/default?alt=json-in-script&callback=mbtlist'></"+"script>");
</script>

<!-- ######### Styles for Look ############# -->

<link href='http://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css'/>
<style>
.mbtlist {list-style-type:none;overflow:hidden}
.mbtlist li {margin:0px auto 20px auto; clear:both; color:#666; font-family:Helvetica; font-size:12px; border-bottom:1px dotted #ddd;}
.mbtlist .mbttitle {font-family:oswald; font-size:16px; color:#0080ff; font-weight:normal; text-decoration:none;}
.mbtlist .mbttitle:hover  {color:#00A5FF;}
font-family:georgia; font-size:15px; font-weight:bold}
</style>

 

As you know we have divided the above script in three parts and I will discuss it accordingly

1. Writing the Callback Function

1 Here we first declared some default values to variables. In order

  • ListBlogLink will fetch the Browser address URL if no blog link is mentioned;
  • ListCount will display the 5 latest posts recently published.
  • TitleCount will cut/chop the title if it's length exceeds 70 characters.

Note that the user can customize these options by re-setting them during Invoking. If incase the user does not mention any values the defaults will be used.

2 Next since we are printing the Title list we therefore first printed <ul class="mbtlist"> just before starting the function.

3 We then started a for Loop from 0 till the user assigned List count.

for (var i = 0; i < ListCount; i++)

4 After declaring some variables for printing the list, Title and title URL we started our first loop to fetch the "Title URL"

for (var j = 0; j < json.feed.entry[i].link.length; j++) {
if (json.feed.entry[i].link[j].rel == 'alternate') {
break;
}
}
ListUrl= "'" + json.feed.entry[i].link[j].href + "'";

  • We first run a loop from 0 till the full length of [ ] link
  • We then run a condition to check if rel has value "alternate". If the value didn't match, the loop will end rendering else it will continue to fetch the href value in that node.
  • the value of href (i.e. Title URL ) fetched is then stored inside ListUrl

5 Next we wrote the script to extract the Title String i.e. Title text.

if (json.feed.entry[i].title!= null)
{
ListTitle= json.feed.entry[i].title.$t.substr(0, TitleCount);
}
  • We first test a condition to see if Title is not empty. This is important because if you publish an article without a title in your blog then the above script will crash and wont work. Therefore this condition will make sure to fetch the Title string only when the title exists for a post.
  • We then extracted the title text and stored it inside ListTitle
  • In order to make sure the title length is not too long, we used a JavaScript String substr() method  to display only the first few characters up to the length mentioned in TitleCount

 

INFO
The substr() method extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters.

6 Later we printed the list inside <li> tags using the variable listing

7 Finally we closed the unordered list outside the first loop using

document.write("</ul>");

Note that the entire data is extracted and printed inside <li> tags. The loop which is fetching and printing the list is this:

<ul> tag is printed here

for (var i = 0; i < ListCount; i++)

{

<li> tags are printed here

}

</ul> tag is printed here

This loop itself is enclosed inside the <ul> and </ul> tag. Thus the Recent Posts are displayed! :)

2. Invoking the callback Function

You might have noticed that this time the invoking section of the code looks a little unfamiliar. Previously:

<script type="text/javascript" src="http://www.mybloggertricks.com/feeds/posts/default?alt=json-in-script&callback=mbtlist">
</script>

Now:

document.write("<script src='"+ListBlogLink+"/feeds/posts/default?alt=json-in-script&callback=mbtlist'></"+"script>");

We simply printed the Inline JavaScript code inside a document.write() function just to give our widget a more dynamic control. This way we can assign variables inside the Invoking section and give more control to the user. This makes the code easy for the user who can then easily assign custom values for ListBlogLink , ListCount and TitleCount .

3. Defining CSS Styles

No one would like a dull and grey list unless you add some custom colors to it.

recent posts with styles

Without the style sheet the widget will look like this:

recent posts without styles


I am sure no one would like it without colors therefore in the last part we added some CSS touch to the list! :)

Have Questions?

I tried my best to make it as simple as possible, if incase you could not understand any step, feel free to ask your question by posting your comment below.

In our next tutorial, we will explain how to display recent posts by specifying a Label. Recent Titles will display based on the category specified by the user. Happy weekend buddies! =>

How To Add Separate Description For Every Page In Blogger

how to add separate descripiton to each post in blogger

After publishing many posts about seo I am back with a new one. This post is about adding a separate description to each page in blogger. You might be thinking of what do I mean by separate description, if it is so then let me explain it.

Why Do I Add A Separate Description To Each Of My Pages?

Let me tell you its benefits first before I explain why is it important to add a separate description for each page. You will realise the importance of a separate description by reading its benefits.
  • The first benefit of adding a separate description to each page is it lets you to get maximum results from search engines because the description you add to a page contains long tail keywords and hence when someone search something similar to your keywords they see your blog in search results.
  • Second benefit of adding a separate description to each page is it helps you to fix duplicate description errors in webmaster tools specially in Bing webmaster tools.
  • As long keywords are used in description therefore it increases the relevancy of your content with your keywords which is good for seo.


All the above benefits makes it important to have a separate description for each page in blogger. So this post is dedicated to blogger seo. Follow below steps to add a separate description to each and every page of your blog.

How To Add Separate Description To Every Page In Blogger?

Before we start adding a separate description to each page we have to touch robots.txt first. We have to add new robots.txt to tell robots to index all our pages except not found. So follow below steps to add new robots.txt to your blog.

Step 1: Go to Blogger Dashboard > Settings > Search preference > Crawlers and indexing and edit Custom robots.txt.

how-to-add-robots-text-in-blogger

Step 2: Copy below robots.txt and replace it with the existing text.

User-agent: Mediapartners-Google
Disallow:

User-agent: *
Disallow: /search
Allow: /

Sitemap: http://101helper.blogspot.com/sitemap.xml

robots.txt for blogger

Replace http://101helper.blogspot.com with your blog url.

Above robots text tells robots to index all pages except not found pages. So static pages(contact, about, sitemap etc), posts and category pages will be indexed.

Step 3: Save changes and you are done, now follow next steps to start adding separate description to your blog pages.

Step 4: Turn on search description for each post by following this tutorial: How To Enable Search Description In Every Post In Blogger

That's it we are almost finished now you have enabled separate description for each blog post. Now its time to add description to category pages as we have told robots to index all pages so category pages will also be indexed.

Step 5: Copy below piece of code and paste it in <head> section of your template:

<b:if cond='data:blog.url == &quot;Your category url&quot;'>
<meta content='Your description or long tail keywords' name='description'/></b:if>

Step 6: Replace Your category url with the link of category for which you are adding description and Your description or long tail keywords with appropriate description or long tail keywords.

Step 7: Save template and you are done.


Explanation(Must Read):

Here I am going to explain what we actually did in above steps so it will make clear and easier for you to understand how it works and how to add separate description for categories.

The first thing we did in first three steps was telling robots to index all pages. E.g contact, about, sitemap, categories and posts pages.

In the fourth step we enabled a separate description for each post and static pages(contact, about, sitemap etc).

In step 5 we added a separate description for category. We used some code which will show that description only on the specified category. Category is specified by its url.

So now you have to add the code mentioned in step 5 for each category and make changes for it.

That's it you are 95% done one last thing which you have to do is to replace your homepage meta description tag with the below meta tag:

<b:if cond='data:blog.url == data:blog.homepageUrl'><meta content='Your Blog Description' name='description'/></b:if>

This code will limit your homepage description to only homepage because of the highlighted code.

Now you are 100% done and ready for search engines. I hope you like this tutorial.

Please leave a comment below if you have any problem. Share this post with others.

Search Tags: Fix,remove duplicate description blogger,google,bing webmaster tools duplicate description remove tutorial,fix webmaster tools errors in blogger optimize for search engines tutorials for blogger,boost website traffic,organic traffic for blogger blogs.

Display "Label List" using Blogger JSON Feeds

extract label list from blogger JSON feedsIn Part3 you learned how a simple JavaScript technique is used to fetch data from a blogger JSON file and you also exercised a simple example of retrieving the total posts count published by a blog. Today we will discuss how to print the "Total Label List" created inside a blog to categorize content. You will be able to display all Category Links or Label links used by a blogspot blog. It is almost the same technique Blogger uses to create the Labels Widget or Labels Cloud gadget. This tutorial is the first of its kind to be shared online so make sure you read it carefully to excel playing with JSON feeds the best you can.

Before proceeding, if you are new to Blogger Labels and its use then take a minute to read this post first:

Note: Click the Button below to see full list of topics under discussion.

Topics List

JSON Feed gives Only Label Names

The full list of categories used by an blogspot blog is stored inside the [ ] category array node. Each element inside the array has a Name/Value pair where the Name is called "term" and the Value is the Label-name that we need as shown below.

Category Array node in JSON feed 

You can observe that no information is given regarding the Label Links/URLS, we just have access to the Label Names. In this tutorial you will learn how to print the above list along with their Specific URLS, so lets get to work!

Also observe that the information we need is stored inside json.feed.category[k].term path where K is the array element position.

First we will access json then > feed then > category and then > each term value.

Extracting Category List From JSON Feed '[ ] category' Node

To achieve this purpose we will first write a JavaScript code to retrieve and print the Labels List as plain text, later we will discuss how to give each Label its proper URL.

INPUT

 

<!-- ######### Writing Callback Function ############# -->

<script type="text/javascript">
function mbtlist(json) {
var  ListTag= "";

for (var k = 0; k < json.feed.category.length; k++)
{
ListTag +=  "<li>"+json.feed.category[k].term+"</li>";
}

var listing = "<H3>CATEGORY LIST :</H3><ol class='mbt-ol'>"
+ListTag+
"</ol> " ;

document.write(listing);
}
</script>

<!-- ######### Invoking the Callback Function ############# -->
<script type="text/javascript" src="http://www.mybloggertricks.com/feeds/posts/default?alt=json-in-script&callback=mbtlist">
</script>

<!-- ######### Styles for Look ############# -->

<style>
.mbt-ol {width:300px; margin:10px;}
.mbt-ol li {border-bottom:1px dotted #ddd; padding:5px; }
</style>

OUTPUT

plain Label List

NOTE
I have shown only first 11 Labels because the actual list is 45 which would fill up a big space so we are just sharing a small view. To see the actual list copy our code above and paste it inside our HTML Editor to see the code in action.

This is how the above script works:

PS: (I assume you have already exercised part3 of our tutorial series):

1. First we created a variable (ListTag) for storing Label name in it and assigned it an empty value ( " ").

2.  Then we ran a FOR Loop starting from k=0 and going up to json.feed.category.length  (Can you answer why? :) )

3. We then assigned ListTag to the path where each label is stored. Since the [ ] category node has multiple sub-nodes that is why we ListTag assigned with a += sign and not ===. So that each label name is kept on added to ListTag. The k inside category [k] is done so to ensure we access the correct sub-node term value each time sequentially. Each time the loop will run it will fetch one value of term and store it inside ListTag, this way all the Label list is retrieved! I have enclosed json.feed.category[k].term inside <li> tags so that each value is printed nicely inside an ordered bullet list.

INFO:
A string is always enclosed inside (") double quotes in JavaScript and variables are always enclosed inside (+) signs. This is why we enclosed <li> inside "<li>"  and the Label list variable inside +... +

4. Finally we stored the results inside the variable listing and printed it. In order to make sure the List is displayed nicely we added some styles to the <ol> ordered list using the class name 'mbt-ol'.

Assign Hyperlinks to Plain Label List

Now that you have understood the concept on how this list is displayed, lets make a small modification to the code above to replace each label with its hyperlink.

In blogger all label have the following URL structure:

http://Blog-Name.blogspot.com/search/label/Label-Name

In our case it would be:

http://www.mybloggertricks.com/search/label/Label-Name

In the code above replace this part:

ListTag += "<li>"+json.feed.category[k].term+"</li>";

with this href attribute:

ListTag +=  "<li><a href='http://www.mybloggertricks.com/search/label/" + json.feed.category[k].term + "'>"+json.feed.category[k].term+"</a></li>";

OUTPUT

Label List with Links

That simple! Go on test and play with the code to see it Live for yourself. :)

How to Display only first 10 Labels?

If you don't want to display the full list of labels then you can choose to display selected labels by making the following modification

Replace this code

for (var k = 0; k < json.feed.category.length; k++)
{

with this condition:

for (var k = 0; k < json.feed.category.length; k++)
{ if (k < 10)

To display 30 items just replace 10 with 30 and so on.

Need Help?

In our coming tutorials we will be digging deep inside the [ ] entry array where all your blog posts data is stored. Some of you also asked how to display more than 25 items per page in JSON Feed, I will discuss them all in my coming tutorials so stay tuned with all coming updates. Feel free asking as many questions as you can to better understand each and every logic. I hope these step-by-step programming guide may help most of you in understanding some basic and core logics.

Peace and blessings buddies! :>

How To Add Map In Blogger Contact Or About Page

maps-in-blogger-website-blog

Bloggers are advancing day after day and making their blogs professional by adding pages like privacy policy, contact page, about page, terms and conditions page etc which are seen in professional websites so they are trying to make their blogs look more professional to impress visitors because such things improve blogs reputation. One more thing which can improve a blogs reputation and make it look more professional is a map which shows the location of the blog. So if you have a business website converted to dot com then you can add a map in your website or blog's about or contact page and your location to your visitors as well as make your website or blog more professional. So this post is all about adding Google map in contact or about page of your blog. Follow below steps to add a map in your blog's page.

How To Add A Map To Contact Or About Page In Blogger?

Step 1: Go to Google Maps and search your location.

how-to-add-map-in-blogger-contact-or-about-page

Step 2: Click on Share below directions:

how-to-embed-map-in-blogger-101helper

Step 3: Click on Embed Map.

how-to-embed-map-in-blogger

Step 4: Set your location by clicking inside map and moving it, change the size of your map by clicking on selector beside code. Available sizes are small, large, medium and custom. Choose custom if you want to create a custom size map.

embed-map-in-blogger

Step 5: Copy code of your map and go to blogger dashboard.

Step 6: Click on pages and edit your about or contact page in which you want to add map.

Step 7: Switch to the Html editor of your page:

how-to-switch-to-html-page-editor-in-blogger

Step 8: Paste the code copied from map page.

Step 9: Click on Publish and you are done!

You can add the map in a gadget too at the bottom of your blog. All you have to do for this is to add the map code in the layout. So if you want to add a map at the bottom of your blog then go to blogger > layout > Add a gadget > Html/Javascript, Paste code of map and click on save!

I hope you like this post and you found it helpful. Share it with others. Thanks for visiting 101Helper.

Search Tags: Map for blogger,add a map in blogger,add a map to website,Google maps gadget for blogger,embed map in blogger blog,Add map in contact page,101Helper blogger tutorials.

How To Customize Scrollbar In Blogger


One of the most wanted thing in a template is responsiveness to impress visitors. It play a very important role in reputation of a blog. As basic things of a responsive template are bold use of colors, smooth scrolling, impressive menu therefore people usually focus on them but it is not just about that because several other things are also responsible for a responsive design like a stylish scrollbar. I don't thing scrollbar needs introduction everyone knows what a scrollbar is. So this post is about customizing scrollbar in blogger by CSS. I will share some stylish and impressive scroll bars below, choose yours and add it in your blog.

How To Customize Scroll bar In Blogger?

Step 1: Go to blogger dashboard, navigate to template and edit html.

how-to-edit-template-in-blogger

Step 2: Click inside the code and search for below piece of code:


]]></b:skin>


Step 3: Choose a customized scrollbar and copy its code.

Style 1: 

customized-scrollbar-style-1-blogger
body::-webkit-scrollbar-track {
 -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
 background-color: #F5F5F5;
}
body::-webkit-scrollbar {
 width: 12px;
 background-color: #F5F5F5;
}
body::-webkit-scrollbar-thumb{
 border-radius: 5px;
 -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,.3);
 background-color: #ccc;
}

Style 2:


customized-scrollbar-style-2-blogger
body::-webkit-scrollbar-track {
 -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
 background-color: #F5F5F5;
}
body::-webkit-scrollbar {
 width: 12px;
 background-color: #F5F5F5;
}
body::-webkit-scrollbar-thumb {
 border-radius: 0px;
 -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,.3);
 background-color: #35BB6E;

}

Style 3:

body::-webkit-scrollbar-track {
 -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
 background-color: #F5F5F5;
}
body::-webkit-scrollbar {
 width: 14px;
 background-color: #ffffff;
}
body::-webkit-scrollbar-thumb {
 border-radius: 5px;
 -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,.3);
 background-color: #3993E2;
}

Step 4: Paste it above the below piece of code.

]]></b:skin>


Step 5: Click on save template and you are done.

Custom Style:

You can make a custom scrollbar too by making changes according to your needs in highlighted fields above. Read explanation of each code function below:

Color of scroll bar:

To change color of your scrollbar replace #3993E2 with your chosen color code in  background-color: #3993E2; in the third style.

Edges of Scroll bar:

To change radius of edges of your scroll bar make changes in border-radius: 5px; in the third style.

Background color of scrollbar:

To change background color of your scroll bar make changes in background-color: #ffffff; in the third style

How to get code of a color? Click here to use color code generator!

I hope you liked this post. If you have any problem or suggestions please leave a comment below. I will get back to you. Follow and subscribe to get news  about new posts. Don't forget to share this post. Thanks for visiting 101Helper.

Search tags: Blogger tips and tricks,Responsive blogger template design,CSS tricks blogger,Stylish scrollbar codes for website or blog,Impressive blogger template design,How To Customize Scrollbar In Blogger

Stop Blogger from Redirecting Blogspot to Country Specific URLs

Let's say you're from France and have set up - just for examples sake - a blog called frenchlitgeek.blogspot.com where you share your thoughts and insights on French literature. Now, with Google's country specific redirection in Blogger, you might be redirected to frenchlitgeek.blogspot.fr when you try to access your site. The thing is, you perfectly liked the .com and didn't sign up for the .fr but you find yourself being directed there. Sure, your blog works and all but you also wonder why.
blogger country specific redirection

Why Did Google Do This?

Google has always supported the expression of views, and they stated as much on their official blog. In the post Free expression and controversial content on the web, which was published in 2007, it said "Our world would be a very boring place if we all agreed all the time. So, while people may strongly disagree with what someone says, or think that a particular newspaper is total nonsense, we recognize that each of us have the right to an opinion."

The post continued, "We also know that letting people express their views freely has real practical benefits. Allowing individuals to voice unpopular, inconvenient or controversial opinions is important. Not only might they be right (think Galileo) but debating difficult issues in the open often helps people come to better decisions".

blogspot country redirection

While the company is clearly on the side of people freely expressing their opinions, they also believe that a line has to be drawn somewhere. Then again, for a company providing services in over 100 countries around the world and each with their own national laws and cultural norms, it's surely difficult for a company like Google to decide where to draw boundaries.

However, there are cases like child pornography which is illegal in just about any country where decisions are clear cut.

For a company whose products are "specifically designed to help people create and communicate, to find and share information and opinions across the world", how does Google deal with this challenge?

One of the most challenging areas where Google deals with issues regarding free expression is in Blogger, their content generation platform. Since Google can't check what you've written before you publish, they rely on active vocal users who are diligent in alerting the proper if a post borders on offensive. Then again, that in itself is a tricky issue as well because what one person may view as offensive, another might not.

In other words, it's always a work in progress with Google.

Fast forward to January 9, 2012 when Google announced it was making changes to the Blogger platform with regards to censorship. That said change would make use of a country specific domain to the Blogger platform. Doing this would allow Google to censor and remove content specific to a certain country.

In their announcement, Google said: "Migrating to localized domains will allow us to continue promoting free expression and responsible publishing while providing greater flexibility in complying with valid removal requests pursuant to local law. By utilizing ccTLDs, content removals can be managed on a per country basis, which will limit their impact to the smallest number of readers."

The move by Google come after pressure from countries like India that are working on hunting down content on social media sites which are considered inappropriate. Also, the move followed closely on the heels of Twitter's new censorship policies.

Since Google aims to "help people create and communicate, to find and share information and opinions across the world", it would be strange to take down a post that was just banned in a certain area. In essence, with country specific redirection, a piece of content can still be accessible by the world save for the country where it was blocked.

How Would Country Specific Redirection Affect Your Site?

Of course, not all site owners greeted the country specific URL change with open arms. A few of the issues brought up in regard to the change include:

1. A reduction in social stats. These are your Facebook Likes, Google +1s and so on from your blog posts. They might be reduced because the URLs from one blog post will be different depending on where your readers are from.

2. A problem with external commenting platforms. If you use Disqus - for example - for your comments section, then you might run into trouble because blog URLs will be different even if essentially the page being accessed is just the same.

3. A slight problem with AdSense earnings. Some users have complained about seeing a dip in their earnings when their pages are served through country specific domains.

4. An issue with link juice. You want external sites to link to you and not your country specific URL. But the issue here is that you can't control how others link to your page. They might use the top level domain or they might use the country code top-level domain.

Stop Blogger from Redirecting to Country-Specific Domains

If country specific redirection affects important factors such as traffic and link juice, and you need those in order to rank well, what can you possibly do? Well, thankfully, Google has provided a way to get around this. All you have to do is add an ncr/ to the end of the URL - ncr here stands for No Country Redirect. So basically, it goes frenchlitgeek.blogspot.com/ncr/.

That solution is great but do you want your users to always have to do that every time they visit your blog? To eliminate that hassle, and for the good of your site statistics, a simple redirection script will do the trick. Here's how:

1. Log in to your Blogger account.

2. Click on Template → Edit HTML.

3. Find the <head> tag in the HTML editor by opening the search box using Ctrl + F.

blogger country redirection

4. Copy the redirection code seen below after the <head> tag.
<script type="text/javascript">
var blog = document.location.href.toLowerCase();
if (!blog.match(/\.blogspot\.com/)) {
blog = blog.replace(/\.blogspot\..*?\//, ".blogspot.com/ncr/");
window.location.replace(blog);
}
</script>

5. Click on "Save Template".

Credit: labnol.com

And that's it! Whenever someone accesses your Blogger, they'll be taken to the top level domain rather than the country specific one.

Extracting Data From Blogger JSON Feeds using JavaScript

parsing json in javascriptIn part2 you learned how to view non-readable JSON code in friendly format, today we will start discussing the scripting section. Here you will learn how to use JavaScript to extract information related to a specific Blogspot blog from its JSON Feed file and how simple JavaScript logics, loops, iterations and built-in functions can help you easily retrieve JSON Feeds from Blogger Data API. You will learn several methods to query a blog's public feed and get the resulting entries returned as JSON objects in an extremely way. I strongly recommend that you read w3schools JavaScript basics to understand this tutorial better. No advanced knowledge is required except familiarity with basic programming syntax. Lets get started!

Note: Click the Button below to see full list of topics under discussion.

Topics List

Tip:
You can use our HTML Editor Tool for testing all scripts shared throughout this tutorial series.

Types of Blogger JSON Feeds

There are two types of JSON feeds supported by Blogger blogs. i.e.

Posts Feed

All blog Posts data excluding the Static Pages, is stored inside the Post Feed.

We have already discussed in Part2 what blog information is contained inside a Post feed, please refer that. The Post JSON file is located at the following URL:

http://www.Your-Domain.blogspot.com/feeds/posts/default?alt=json

In our blog case it is:

http://www.mybloggertricks.com/feeds/posts/default?alt=json

Comments Feed

All blog comments data is stored inside the Post Feed. This JSON file is located at the following URL:

http://www.Your-Domain.blogspot.com/feeds/comments/default?alt=json

For example our blog comments JSON Feed is stored at

http://www.mybloggertricks.com/feeds/comments/default?alt=json

Parsing JSON feeds in JavaScript

Parsing in other words mean taking a set of data and extracting meaningful information from it. In blogger the following steps are performed for retrieving JSON feeds of a Public blog:

1. Converting json to json-in-script

First the JSON file is converted to a JavaScript supported format by simply replacing the parameter json with json-in-script in the JSON Feed URL

INFO
By using a "json-in-script" parameter, blogger encloses the JSON code inside the following JavaScript Function:

gdata.io.handleScriptLoaded( );

All the JSON code is automatically inserted inside this function as shown below:

gdata.io.handleScriptLoaded(JSON-Code-Here);

So the JSON URLs are transformed to this format accordingly:

For Posts:

http://www.mybloggertricks.com/feeds/posts/default?alt=json-in-script

For Comments:

http://www.mybloggertricks.com/feeds/comments/default?alt=json-in-script

Without this step the callback function would not work.

Tip: You can paste the above URLs in your browser address bar to see the difference.

2. Calling the Callback Function

The callback function is the sole of your JSON feeds retrieval process. This is where you request the browser what data to fetch from the server. All programming logic is written inside this JavaScript function.

You can give it any name you like. For demonstration purpose we will be using the name "mbtlist"  throughout this tutorial series.

The callback function name is passed as a parameter inside the JSON URL in order to call it. So our complete new URL would be:

For Posts:

http://www.mybloggertricks.com/feeds/posts/default?alt=json-in-script&callback=mbtlist

For Comments:

http://www.mybloggertricks.com/feeds/comments/default?alt=json-in-script&callback=mbtlist

To invoke our Callback function the above URL will be inserted inside a JavaScript src attribute as shown below:

<script type="text/javascript" src="http://www.mybloggertricks.com/feeds/posts/default?alt=json-in-script&callback=mbtlist">
</script>

3. Writing Code for the Callback Function

Finally here comes the actual coding part where you will learn how to display the total number of posts and comments published in a blog.

Lets analyze the simple snippet below.

INPUT


<!-- ######### Writing Callback Function ############# -->

<script type="text/javascript">
function mbtlist(json) {
for (var i = 0; i < json.feed.entry.length; i++)
{
var TotalPosts = json.feed.openSearch$totalResults.$t;
  }

var listing = "Total = " +TotalPosts+ " Posts" ;
document.write(listing);
}
</script>

<!-- ######### Invoking the Callback Function ############# -->

<script type="text/javascript" src="http://www.mybloggertricks.com/feeds/posts/default?alt=json-in-script&callback=mbtlist">
</script>

OUTPUT

Total = 1418 Posts

Script Illustration:

extracting data from JSON

This is what the above script does:

1 First we defined a callback function  i.e. "function mbtlist(json){ }" and passed inside it json as parameter.

2 Next we will run a FOR loop to travel across the JSON hierarchy till its last array which is [ ]entry. The loop will cover the full length of the JSON content, therefore we defined it as:

for (var i = 0; i < json.feed.entry.length; i++)
INFO

"json.feed.entry.length" means:

First go to JSON node > then go to feed > then entry > then till its full length.

 

3 Next we will access the object which contains the total posts count. This data is provided by the object: openSearch$totalResults

In order to access this node we will choose this path:

json.feed.openSearch$totalResults.$t;

We saved this data inside a variable TotalPosts . The variable names are chosen by us, you can give them any name you like.

To make the output look pretty we added some text (Total and Posts ) to it which you can change with any message you like.

4 We then closed the loop because our search is complete and we have reached the specific object node we wanted. Now we will simply print this data inside another variable that we assigned as listing

5 Finally we Invoked/called our function to come and perform its task. Nothing will display or print if you don't add this last part. 

That's it! :)

Display Total Comments Count

Exact Same method as shared above because the { } feed object has exact same nodes for both Comments and Posts feeds. The only thing that needs to be changed is replacing the Post Feed URL with Comments Feed URL. Which means replacing posts with comments in the invoking part.

INPUT


<!-- ######### Writing Callback Function ############# -->

<script type="text/javascript">
function mbtlist(json) {
for (var i = 0; i < json.feed.entry.length; i++)
{
var TotalPosts = json.feed.openSearch$totalResults.$t;
}

var listing = "Total = " +TotalPosts+ " awesome comments!" ;
document.write(listing);
}
</script>

<!-- ######### Invoking the Callback Function ############# -->

<script type="text/javascript" src="http://www.mybloggertricks.com/feeds/comments/default?alt=json-in-script&callback=mbtlist">
</script>

Note: Changes made are highlighted in red font.

OUTPUT

Total = 41028 awesome comments!

 

Need Help?

This tutorial can become more productive if you ask questions to clarify any confusion or doubts you may have in understanding any logic. This is the most detailed tutorial on extracting data out of blogspot JSON feeds ever shared online, so feel free asking questions so that we may better assist you. All these tutorials are shared with sole purpose in mind to better help students and young designers with programming skills. I wish we succeed with this sincere help. Wish you all a great coding experience. Peace and blessings buddies! Catch you back with part4! =)