Those following this blog would have seen that I tend to use the show/hide posts option to condense the amount of information shown. Although it has worked pretty well, I was always irked by the fact that if my posts were really long, clicking on the "Show/hide full post" option on my blog would tend to roll up the current post and show the posts at the bottom leaving the viewer behind instead of returning them back to the initial post in reduced form.
Poring through the code and referring to W3C HTML specs, I managed to find a simple way to redirect the reader back to the top alogside the hidden post with the use of anchors.
The original technique used on this site is the one described on the Blogger help pages here. The only difference is now I've made use of the span id as an anchor for redirection. Only one line is added to the Javascript function to make this hack work. The original function is written like so
The focus is in the "if" section of the function which evaluates such that when the post is currently being shown (i.e. is equal to "postshown") then the function will hide the post by setting the className to "posthidden". Here I add a line to make redirect the browser to the span id which is my anchor. The edited code is as follows
<script type="text/Javascript">
function expandcollapse (postid)
{
whichpost = document.getElementById(postid);
if (whichpost.className=="postshown") {
whichpost.className="posthidden";
}
else {
whichpost.className="postshown";
}
}
</script>
What this basically does is to take the postid (which will become our span id when we write the post) and redirect using the location.hash function.
<script type="text/Javascript">
function expandcollapse (postid) {
whichpost = document.getElementById(postid);
if (whichpost.className=="postshown") {
whichpost.className="posthidden";
self.location.hash = postid;
}
else {
whichpost.className="postshown";
}
}
</script>
This change would only work if the "Show/hide full post" link is located near the top of the post. If the option is halfway through the post, there is still another way to redirect readers back to the top of the post. In each post with the show/hide option, add an additional anchor under a different name from the usual span class method so as not to have conflicting section names. For example, I could add an anchor to top of this post with the following code
Then edit the javascript function in the template so as to be able to receive two parameters like so with the first parameter being the span id to indicate which portion of text to hide and the second parameter to indicate the anchor<a name="postanchor"></a>
Then just change the blogger tags of posts to indicate the span areas and anchor
<script type="text/Javascript">
function expandcollapse (postid,anchorid) {
whichpost = document.getElementById(postid);
if (whichpost.className=="postshown") {
whichpost.className="posthidden";
self.location.hash = anchorid;
}
else {
whichpost.className="postshown";
}
}
</script>
<a href="javascript:expandcollapse('<postid>','<anchorid>')">
Show/hide full post</a>
Here's an example of how a posts' structure would be like if using the second method mentioned above (using HTML view editing)
<a name="thispostanchor"></a>
.....
First paragraph of post or abstract
.....
<span class="posthidden" id="thispostid">
.....
Rest of the post
.....
<a href="javascript:expandcollapse('thispostid','thispostanchor')">
Show/hide full post</a>
..... Show/hide full post
No comments:
Post a Comment