Reputation: 1948
I'm a page that redirects to itself. (With next day and previous day arrows) At document.ready the page displays the new day.
The days are stored in a localStorage like this :
for(var i = 0;i<status.length;i++)
{
localStorage["Day" + i] = status[i].name;
}
I want the default day that shows every time is "Day0" then when the user clicks next or previous the other day is showed.
$(document).ready(function () {
$("#CurrentDay").empty();
$("#CurrentDay").append(GetCurrentDay());
});
function GetCurrentDay() {
localStorage["CurrentDay"] = localStorage["Day0"];
return localStorage["CurrentDay"];
}
I made the arrows call these functions:
function GoToNextDay() {
if (i != localStorage["SchoolDays"]+1) {
i = i + 1;
localStorage["CurrentDay"] = localStorage["Day" + i];
}
else {
localStorage["CurrentDay"] = localStorage["Day" + 0];
}
}
function GoToPrevDay() {
if (i != 0) {
i = i - 1;
localStorage["CurrentDay"] = localStorage["Day" + i];
}
else {
localStorage["CurrentDay"] = localStorage["Day" + localStorage["SelectedDaysNumber"]];
}
}
I'm wondering how can I store the value "i" coz when I initialize it with "0" at the beginning of the script, it keeps returning to 0 whenever the page is called, and if I made it with no value how can I know the place I'm starting at ?
Upvotes: 0
Views: 411
Reputation: 477
you can either use a hash (#) in the url to let your script know what day you are on now, or you can save that information in a cookie.
note that if you use a hash symbol in the URL you can actualy send a link to someone else to a specific day where as in cookies its a per "session" basis meaning only that user's browser will remember what day is active at the moment.
so for example:
www.example.com/#Day0
will be the URL for Day0
www.example.com/#Day1
will be the URL for Day1
then in your code you need to do something like this:
var current_day = window.location.hash;
and use that variable to init your array.
whenever someone clicks to go to another day simply do a history push to the location like
var url = "#" + day_to_display;
if (history.pushState)
history.pushState("", "", url);
else
window.location = url;
Upvotes: 1
Reputation: 3226
You can use localStorage or globalStorage to store the data:
if(typeof localStorage['userName'] === "undefined") {
// defining the localStorage variable
localStorage.setItem('userName', 'taranfx');
}
alert("Your user is: " + localStorage.getItem('userName'));
OR you can use cookies to store the value even after refresh of the page:
Cookies can be created, read and erased by JavaScript. They are accessible through the property document.cookie. Though you can treat document.cookie as if it's a string, it isn't really, and you have only access to the name-value pairs.
If I want to set a cookie for with a name-value pair 'ppkcookie1=testcookie' that expires in seven days from the moment I write this sentence, I do
document.cookie =
'ppkcookie1=testcookie; expires=Thu, 2 Aug 2001 20:47:11 UTC; path=/'
This is a very strict syntax, don't change it!
Also, even though it looks like I'm writing this whole string to the string document.cookie, as soon as I read it out again I only see the name-value pair:
ppkcookie1=testcookie
http://www.quirksmode.org/js/cookies.html
Upvotes: 3
Reputation: 673
Store it in localStorage. When launching the script for the first time, check if typeof localStorage['your_var'] == "undefined"
, if true
set it to 0, if false, use that value.
Upvotes: 1