Save browsing history to a cookie
Sometimes we may want to keep a record of every page that a user has visited on our website.
To do this we can write: https://raw.githubusercontent.com/finsweet/jqueryCourse/main/cookies/ex19.js?actionable=true
The code above will first check if there is a cookie named history. If it finds this cookie it will retrieve its value which is always text that we can convert to an array.
We can add the current page to this array.
It will then store this new array as text in the history cookie.Because cookies can only be store text we use the function JSON.stringify() to convert our array to text.
Conversely we use the function JSON.parse(pages) to convert our stored text to an array.If the cookie is non existent then we will create the cookie and add the current page as its first value like so:Cookies.set('history', JSON.stringify([current_page]))
$(document).ready(function(){
XT1
});
</script>
Live demo
$(document).ready(function() {
XT1
});
</script>