Home > HTML/CSS/JS, Web > Javascript View counter!(Client only)

Javascript View counter!(Client only)

Hello ppl that use AJAX and server-side to create view-counters!

I have something to tell you all!(You still need  some server side support)

HTML5 gives YOU the option to use localStorage besides other things like god dam easy configuration storage(Check out HTML5 and you will see what i mean, i will not cover this aspect in this post)

The following script(JS) saves a local_views variable with localStorage.(You can create a AJAX function to send the views to your server,or you can just use like “You have visited this page:12 times”)

Hope you find it useful.

//try to access to local storage
if (localStorage){haslocal = "y";}else{haslocal="n";}
function store(key, value)
{	
	if (haslocal="y")
	{
		localStorage.setItem(key, value);
	}
	else
	{
		throw("Local Storage not suported.");
	}
}


function record()
{
	if (haslocal="y")//test for localStorage Support
	{
		if (localStorage.local_views==null)//if local_views variable isnt set
		{
			localStorage.setItem("local_views", 1);
		}
		else//if local_views is set increment it
		{
			//test if thers a int stored
			if (parseInt(localStorage.local_views))
			{
				localStorage.local_views++;	
			}
			else
			{
				//not a int stored going back to one
				localStorage.local_views = 1;
			}		
		}
	}
	else
	{
		throw("Local Storage not suported.");
	}
	
	
}
function getViews()
{
	if (haslocal="y")
	{
		local_views = localStorage.local_views;
	}
	else
	{
		throw("Local Storage not suported.");
	}
}
//store() is a no-need simplification to store values in localstorage
//record() well RECORDS the views
//getViews() ugh? gets the number of views and stores it in local_views variable
Advertisements
Categories: HTML/CSS/JS, Web Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: