5

I am currently publishing a book series with WP (not in english). Books, chapters and subparts are arranged this way: Each book (category) is divided into chapters (subcategory) which is divided into subparts (post). The website is completely open and requires no user account / login. Comments are managed with the Disqus plugin.

Is there any way to create/update a cookie with navigation information, so when the reader leaves and returns the next day, the site automatically navigates to the last post?

EDIT:

MS Word 2013 has an implementation of a similar functionality. When opening a large document, an info box appears "Welcome back, pick up where you left off." That'd be a nice welcome for a loyal reader.

  • 2
    Your question is an interesting one. I have removed the part where you ask for a plugin, note that asking to recommend a plugin or theme is off topic here. – Pieter Goosen Nov 7 '15 at 15:53
4

I like this question so had a look at it myself. The best way I could think to go about it would be to use localstorage and jQuery to store the current URL and scroll position of the user. Then you could either run a check when they come back to that page or have a button like "continue reading" which would retrieve the URL and the scroll position sending the user to that exact point.

JS

if(typeof(Storage) !== "undefined") {

Check is local storage is supported

var storedResult = localStorage.getItem("location");
var storedURL = localStorage.getItem("url");

Get the Stored URL and Scroll Location

 if (storedURL !== 'undefined' && storedResult !== null) {

Check if the URL is stored var currentUrl = window.location.href; Get the current URL

     if (currentUrl != storedURL) {

Check if the current URL and Stored URL Do Not match.

Send user to stored URL. You would probably want to trigger this on a "continue reading" button or something otherwise you could end up sending users where they dont wish to go.

}
    else if (storedResult !== 'undefined' && storedResult !== null) {

elseif check if the window scroll location is stored.

        $(window).scrollTop(storedResult);

scroll to that location.

}
 } 
    $(window).scroll(function () { 

On scroll event

    var scrolledDown = window.scrollY;
    var currentUrl = window.location.href;

get the window scroll potion and url

    localStorage.setItem("location", scrolledDown);
    localStorage.setItem("url", currentUrl);

store in local storage.

});

 } else {

    //No Web Storage Support.
 }

All together

if(typeof(Storage) !== "undefined") {
//Check is local storage is supported

var storedResult = localStorage.getItem("location");
var storedURL = localStorage.getItem("url");
//Get the Stored URL and Scroll Location

 if (storedURL !== 'undefined' && storedResult !== null) {
     //Check if the URL is stored
     var currentUrl = window.location.href;
     //Get the current URL
     if (currentUrl != storedURL) {
         //Check if the current URL and Stored URL Do Not match.

         // send user to stored URL. You would probably want to trigger this on a "continue reading" button or something otherwise you could end up sending users where they dont wish to go.
     }

    else if (storedResult !== 'undefined' && storedResult !== null) {
        //elseif check if the window scroll location is stored.

        $(window).scrollTop(storedResult);
        //scroll to that location.
    }
 }

$(window).scroll(function () { 
    //On scroll event
    var scrolledDown = window.scrollY;
    var currentUrl = window.location.href;
    // get the window scroll potion and url
    localStorage.setItem("location", scrolledDown);
    localStorage.setItem("url", currentUrl);
    // store in local storage.
});

} else {
    // No Web Storage Support.
}

Demo

JSFiddle: https://jsfiddle.net/fg9uok4L/

Resources

jQuery .scrollTop: http://api.jquery.com/scrollTop/ jQuery .scroll: http://api.jquery.com/scroll/ LocalStorage: http://www.smashingmagazine.com/2010/10/local-storage-and-how-to-use-it/

Not sure on performance with this not tested.

| improve this answer | |
  • Thank you for an interesting suggestion. I will try to implement it, and will come back with further comments / acceptance. – Gjermund Dahl Nov 11 '15 at 10:33
  • Mark's solution successfully implemented at www.trollkrigen.no. The website has afront page, where a welcome-button now is displayed if navigation data is stored in local memory, which takes the guest to the correct page and page height. I have made a few adjustments to the code in order to achieve this. Mark's example has been very helpful. – Gjermund Dahl Nov 14 '15 at 11:06
  • Glad it was some help :) – Mark Nov 15 '15 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.