Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
User-Specific Frequently Visited Pages
Old 02-27-2010, 07:21 AM User-Specific Frequently Visited Pages
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
As many on this forum have done, I have created a number of applications with quite a few options. I've notice, however, that most users tend to favor certain pages. Now, I wanted to give them a means of finding their most popular links, but didn't want to log every page they go to and run massive queries just for a bit of convenience. With browser-side caching now available, I thought that this would be a perfect opportunity to use that feature to allow users to be able to view their personally-most-visited pages.

Along those lines, I've created the following:

JS File: popular_history.js
Code:
function PopularHistory() {
  this.log = function() {
    if (!window.localStorage) return;
    var current_history = [];
    if (window.localStorage.popular_history) {
      current_history = window.localStorage.popular_history.split("\n");
    }
    var found_url = false;
    if (current_history.length > 0) {
      for (var i=0; i<current_history.length; i += 2) {
        if (current_history[i] == location.href) {
          current_history[i+1]++;
          found_url = true;
          break;
        }
      }
    }
    if (!found_url) {
      current_history.push(location.href);
      current_history.push(1);
    }
    window.localStorage.popular_history = "";
    var prefix = "";
    for (var i=0; i<current_history.length; i++) {
      window.localStorage.popular_history += prefix + current_history[i];
      prefix = "\n";
    }
  }

  this.show = function(target) {
    if (!window.localStorage) return;
    if (window.localStorage.popular_history) {
      var html_output = "";
      var top_10_links = [{url:"", count:0}, {url:"", count:0}, {url:"", count:0}, {url:"", count:0}, {url:"", count:0}, {url:"", count:0}, {url:"", count:0}, {url:"", count:0}, {url:"", count:0}, {url:"", count:0}];
      current_history = window.localStorage.popular_history.split("\n");
      var smallest_index = 0;
      for (var i=0; i<current_history.length; i += 2) {
        //If current count > min count of all other top_10, then replace smallest
        for (var j=0; j<10; j++) {
          //Find index of smallest value
          if (top_10_links[smallest_index].count > top_10_links[j].count) {
            smallest_index = j;
          }
        }
        if (current_history[i+1] > top_10_links[smallest_index].count) {
          top_10_links[smallest_index].url = current_history[i];
          top_10_links[smallest_index].count = current_history[i+1];
        }
      }
      for (var j=0; j<10; j++) {
        if (top_10_links[j].count > 0) {
          html_output += '<a href="'+top_10_links[j].url+'" class="popular_history_link">'+top_10_links[j].url+'</a><br>';
        }
      }
      document.getElementById(target).innerHTML = html_output;
    } else {
      document.getElementById(target).innerHTML = 'Error detecting history!';
    }
  }
  this.reset = function () {
    window.localStorage.popular_history = "";
  }
}
var popular_history = new PopularHistory();
popular_history.log();
HTML Code:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Popular History Demo</title>

    <script src="popular_history.js"></script>

    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"><!-- Makes HTML 5 elements available for styling --></script>
  </head>
  <body>
    <nav id="main_nav">
      <a href="index.html">Index</a>
      <a href="index2.html">Index 2</a>
      <a href="index3.html">Index 3</a>
    </nav>
    <div onclick="popular_history.show('history_log');">Show History</div> <div onclick="popular_history.reset();">Reset History</div>
    <nav id="history_log">
    </nav>
  </body>
</html>
For testing, I named the HTML file above to index.html, index2.html, and index3.html. Seems to work to me, but I'd like your input. Specifically, the routine where I track the smallest value and then replace it with a more-frequently-visited page just doesn't sit right with me -- I'd think there's a better way.

Of course, I didn't take the time to pretty it up, yet, but it does demo the feature.

Any feedback?

Oh, and if you like the idea, please feel free to steal it -- though, we all do like a little bit of attribution.

P.S. I'd probably replace if (!window.localStorage) return; in the show function with a message saying to use a browser from this millenium.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
 
Register now for full access!
Old 02-28-2010, 09:01 AM Re: User-Specific Frequently Visited Pages
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
If window.localStorage isn't available, you can attempt to use Flash storage. I've never tried to do this on my own, but there's some plugins available for certain libraries that do this automatically. Here's the one for jQuery: http://code.google.com/p/jquery-jstore/ though I think there's a good implementation for Dojo somewhere also.

Then again, you could just use cookies. Cookies don't hold nearly as much information as DOM storage, but you could easily use sequential cookie names or even use some sort of data pattern within one cookie. Cookies will still hold up to 4K of data, small, but enough for most needs.

Am I misunderstanding what you're doing? Are you caching the whole page or just the URLs?
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 02-28-2010, 11:08 AM Re: User-Specific Frequently Visited Pages
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Thanks. Hadn't thought about Flash storage as a backup! I'm caching URLs with the visit count. I think I'm going to extend it to also store the title tag of the page too and use that as the text of the link when showing the history.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Reply     « Reply to User-Specific Frequently Visited Pages
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.50834 seconds with 12 queries