You do not state why you are doing this on page refresh.
If the expected refresh is user driven could this be done on the server side (you would need to maintain some kind of state of how many times the page has been loaded, potentially in a cookie).
If you are causing the refresh yourself (you have a button or a timed action that in JS you refresh the page) then in that same location you can swap the div (either via ajax or by hvaing loaded all three divs on the page anyway and using jquery to hide/show).
The cookie could store a number that relates to the div. The js or server side then finds the div (perhaps based on nth-child selection in js if they are siblings, if not by looking for a div with id '#div'+index)
So in pseudo code:
- On Page load check if cookie exists,
if so read its value and then set it by incrementing it
in a cycle (i.e. if its hit 3 set it to 1)
- Using 'hide()' hide all divs
- Using 'show()' and the read in value (from the cookie before you changed it)
select the div using nth-child or id
selector and show it
Edit
If you want a javascript only solution assuming your html is:
<div id="div1">
div 1
</div>
<div id="div2">
div 2
</div>
<div id="div3">
div 3
</div>
You could add the following script, in the head. It's using the quirksmode cookie read/write methods. You may wish to replace these with the jquery cookie plugin (or whatever else). It also hides all divs in the 4th from bottom line, more likely you want to use a jquery selector that selects specifically your three:
function createCookie(name, value, days) {
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "; expires=" + date.toGMTString();
}
else var expires = "";
document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
$(function () {
var val = readCookie('div'); // Get last value
createCookie('div', "", -1); // clear cookie
$('div').hide(); // hide all divs
val = (val == null || val == 3) ? 1 : (Number(val) + 1); // incremeent value
$('#div' + val).show(); // show current div
createCookie('div', val, 1); // set value for next time
});
This is a javascript and cookies only solution