1

Let's say, in website, I want to display the notice message block whenever people click any of the link at my website more than x number of times. Is that possible to count with javascript and display the notice message block ? And can we count the refresh times also ? Or can it be only done with server side language like php ? Please kindly suggest. Thank you.

With Regards,

1
  • The counting will be easy, but you need to persist it on the server (db, session, file, etc...) or on the client (cookie, Web Storage, etc...) for it to be useful Commented Mar 24, 2011 at 9:46

4 Answers 4

1

To do something when any link is clicked is best done with JQuery's live:

Description: Attach a handler to the event for all elements which match the current selector, now and in the future.

$('a').live('click', function() {
// Live handler called.
});

Even if you add more links in run time, this will take care of it.

For counting refreshes I would do it with ajax calls on window.load event, or if you want to use new tech - store it locally with Html5. :-)

Sign up to request clarification or add additional context in comments.

Comments

1

You can do that on the client. However, this will be limited to the browser. The simplest will be to store this information in cookies on the client. For instance with jQuery you could simply intercept clicks like that:

$("a").click(function() {
  var clickedUrl = $(this).attr('href');
  // Here you update the cookie for the count of clicks for that A URL
});

3 Comments

limited browser mean, IE 6 ? Others might be ok ?
No, it means that if the user clears his cookies, if he goes to another computer, then the counters will be 0 again. Also if the user goes away without closing his browser, if another user comes he will have the same counters as the other user.
oh okie, at least, if he click next x number of times, it will prompt again for him right ? I want is that, every number of times, it will pop up. Like, every 3 click, it will display different display block.
1

I would either count page refreshes serverside or probably call an ajax function to update the count when the page loads.

If you want to count clicks you may need to bind an event to each link and then for each indivisual button store the number of clicks in global variables...

3 Comments

without binding link, any other ways ? because if i have to bind links, it will be alot of links. That's why
you could bind to ALL links or all "count supported links" binding a single function, that gets an id (or other custom attribute) of the link and updates the count based on that? eg $('.count-supported').live('click', function() { countLink($(this).attr('data-id')); } );
although this may remove href functionality...not sure, could get the contents of href and react to that too...
1

You could register each click event on the document by using:

$(document).click(function()
{

   // Check the number in the cookie and add another
   // click to the cookie

});

Then you could use the jQuery cookie plugin to store that value and check it each time there is a click (in the function above).

here's the cookie plugin: https://github.com/carhartl/jquery-cookie

I threw together a quick example. If you're not worried about doing this from page to page then you don't need cookies, just store it in a variable:

http://www.webdesignandseo.net/jquery/clickcount/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.