Menus

Thursday, May 8, 2014

How To Implement Back To Top Feature in Html/Asp.Net

In many websites having long contents we have seen a “Back To Top” or “Scroll To Top” button when clicked takes you to the top of the webpage. We see this kind of feature normally on the product listing pages of e-commerce websites. Today in this post we will implement the same feature in one of the easiest possible way. To integrate this feature, we need a little HTML, some CSS  for styling and a couple of lines of jQuery code. Using the below method, you can integrate the “Back To Top” feature on your website irrespective of the programming language of your website, be it ASP.Net or PhP.
To start with, we will create a HTML page with the following content in the <body> section.
 
<body>
<h1>Back To Top Demo by Nitesh Luharuka</h1>
<div style="height:1000px"></div>
<span id="back-to-top"><img src='images/up-arrow.png'/></span>
</body>
 
If you notice the above, we have added a div with some height to test our feature. At the end, we have added a “<span>” tag having an image of upward arrow. This arrow will be shown once the user starts scrolling down. To position the image, we will use a bit of CSS.
Here’s the CSS for the button. You need to add this in the head section of your page.
#back-to-top {
     position: fixed;
     bottom: 30px; top: 350px;
 }
#back-to-top img {
 cursor:pointer;
}
Now, our HTML is ready and for adding the code to the image, we will write the below code in our <head> section of the page. If you’ve already included jQuery, then you do not need to include jQuery file again in your project.
<script type="text/javascript">
$(document).ready(function () {
//Hide Back to top button
       $("#back-to-top").hide();
       $(window).scroll(function () {
                            if ($(window).scrollTop() > 200) {
                           $('#back-to-top').fadeIn();
                          }
                         else {
                         $('#back-to-top').fadeOut();
                            } });

 $('#back-to-top img').click(function () {
 $('body').animate({
            scrollTop: 0 }, 1000);
            });
        });
</script>
In the code above, we have done few things. Let me explain them one by one
  • 1st of all, we hide the image when the document loads.
  • In the scroll() event handler, we have ensured that as the user starts scrolling and has scrolled 200px (you can change this), the “Back To Top” image becomes visible to the user.
  • In the click() event handler of the button, we send the user back to the top of the page using a nice animation effect of jQuery.
 

How to allow correct JavaScript references with pages using a Routed URL

Today, every application uses JavaScript. For referencing JavaScript in our .NET application in ASPX pages, we use the following syntax:

<script src="jquery.min.js" type="text/javascript" charset="utf-8"></script>
 
When working with Routed URLs, the above syntax fails to download the referenced 
JavaScript files.
 
 Using the code
To resolve this issue, the scripts should be re-written as below:
<script src="<%=ResolveUrl("~/jquery.min.js") %>" 
type="text/javascript" charset="utf-8"></script>

How To Convert Date Time to “X minutes ago” in jQuery

Today I found a nice plugin with the help of which you can convert any date time on your HTML page to something similar to Gmail/Facebook updates – “5 minutes ago” or “a day ago”. The best part of this plugin is it auto updates the minutes as you’re on the webpage. So, if you have opened the page now and the date shows “1 minute ago”, after 5 minutes the same date will auto update to “6 minutes ago”. In this post, we will see how this plugin works and write a simple HTML using the plugin.

To start with, you can download the timeago plugin from here.This plugin depends on jquery. so the very 1st thing is to include jQuery in our code and then the plugin Javascript.
 
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js">
</script>
<script src="jquery.timeago.js"></script>

Once the plugin files are loaded, you can use the plugin’s timeago() function in any of the following ways -
var date = $.timeago(new Date()); //Displays 'less than a minute ago'
var date = $.timeago('2014-05-04'); //Displays 'a day ago'

You can also call the timeago() function on a specific class on your page as below -
//This will modify the date time on all elements having class as 'time'
$(document).ready(function(){
   $(".time").timeago();
});

How To Convert a Date Time to “X minutes ago” in C#

In this post we will see how we can achieve the same functionality in C#. In this post we will write a C# function that will take a DateTime as a parameter and return us back the appropriate string.
The function to convert DateTime to a “Time Ago” string is as below -
        public static string TimeAgo(DateTime dt)
        {
            TimeSpan span = DateTime.Now - dt;
            if (span.Days > 365)
            {
                int years = (span.Days / 365);
                if (span.Days % 365 != 0)
                    years += 1;
                return String.Format("about {0} {1} ago", years, years == 1 ? "year" : "years");
            }
            if (span.Days > 30)
            {
                int months = (span.Days / 30);
                if (span.Days % 31 != 0)
                    months += 1;
                return String.Format("about {0} {1} ago", months, months == 1 ? "month" : "months");
            }
            if (span.Days > 0)
                return String.Format("about {0} {1} ago", span.Days, span.Days == 1 ? "day" : "days");
            if (span.Hours > 0)
                return String.Format("about {0} {1} ago", span.Hours, span.Hours == 1 ? "hour" : "hours");
            if (span.Minutes > 0)
                return String.Format("about {0} {1} ago", span.Minutes, span.Minutes == 1 ? "minute" : "minutes");
            if (span.Seconds > 5)
                return String.Format("about {0} seconds ago", span.Seconds);
            if (span.Seconds == 0)
                return "just now";
            return string.Empty;
        }
You can call the function something like below-
            Console.WriteLine(TimeAgo(DateTime.Now));
            Console.WriteLine(TimeAgo(DateTime.Now.AddMinutes(-5)));
            Console.WriteLine(TimeAgo(DateTime.Now.AddMinutes(-59)));
            Console.WriteLine(TimeAgo(DateTime.Now.AddHours(-2)));
            Console.WriteLine(TimeAgo(DateTime.Now.AddDays(-5)));
            Console.WriteLine(TimeAgo(DateTime.Now.AddMonths(-3)));
The output looks something like below -
datetime-time-ago-C#