Bookmark and Share

Home Page in Blogger

How to Make a Post or Page Your Homepage in Google Blogger
The question gets asked sometimes on Google Blogger tips sites, how to make a specific page your homepage for Blogger. You can do it with a bit of Javascript. What you do is:
In Blogger design mode, go to Settings, Edit HTML.
In this example I am going to use a homepage, the final part of which is /p/map.html. You need to substitute your own required tail of the url, in place of /p/map.html.
Locate the line in Blogger Edit HTML that begins <title> and on the line following that one insert this:
<script type="text/javascript"> if(window.location.href.substr(7, window.location.href.length-8).indexOf(&quot;/&quot;)==-1) {var v=window.location.href.substr(8)+&quot;/p/map.html&quot;; window.location.href=window.location.href.substr(0,8)+v.replace(/\/\//g,&quot;/&quot;)} </script>
Remember, substitute the tail of the page url you want, in place of /p/map.html
Looks more complicated than it is, what we are doing is:
We’re looking at window.location.href which is the current url.
The first part of window.location.href is likely to be http:// or https:// therefore we start looking at the url from the eighth character because we’re looking for the first occurrence of a slash (/).
Also we want to ignore any slash character that is at the end of the string. So for example if the url is we want to see whether there are any slashes, excluding the three that are in that string.
We therefore look at the url, from the eighth character until the final character minus one. If what we find contains no slashes, then we assume that it is just the base blogger url that has been typed, and not a specific page.
if(window.location.href.substr(7, window.location.href.length-8).indexOf(&quot;/&quot;)==-1)
does just that; starting from the eighth character (character 7) for the length of the url minus eight characters, i.e. the seven before the one we’re starting at plus one at the end, we look for the index character of a slash. If there is none, Javascript will reply with -1.
Right, so if that condition is true, i.e. the result is -1, we do the next bit in two stages so as to make the code a little bit clearer to read than it would be if it were in one. First, we set a variable, v, to the url from its eighth character to its end, and we tag onto the end of it the page we want to be our home page. In the example it is /p/map.html
var v=window.location.href.substr(8)+&quot;/p/map.html&quot;;
Then we get Javascript to set the current url to the existing one for its first eight characters, followed by what we’ve set in variable v, but replacing any occurrence of double slashes by a single slash. We do this replace because if our url was with a slash at the end as opposed to without one, then by taking on /p/map.html we would get a double slash. Actually, that would probably be OK, but we may as well make it nice and neat.
The string v.replace(/\/\//g,&quot;/&quot;) uses a regular expression to do a global replace (which again is belt and braces but then why not?) and since the delimiter character for a regular expression is a slash, we have to escape the slashes within it, hence /\/\// which looks like an attempt to make a picture, but works, trust me.
I hope that’s clear. In any case it works, you just have to substitute your required homepage for /p/map.html – I have used /p/map.html solely as an example.


Post a Comment