Question

I have an HTML5 page which has three buttons. These buttons change various things on my page. For example the first will hide a div, the second is used to change video player to a random color and the third cycles through background images within a folder.

This is the page with the buttons on the side menu (it's partially hidden): http://www3.carleton.ca/clubs/sissa/html5/video.html

My assignment wants us to use the web storage API to save any changes when leaving the page and coming back. So if I change the video player to red and close the window, when I open the window the video player will remain red.

Is this possible at all?

This the the JS for each button:

// Side Menu Function 1
function wideScreen(){
    sidebar = document.getElementById('sidebar');
    banner = document.getElementById('top_header'); 
    body = document.getElementById('body_div');

    if(sidebar.style.display != 'none'){
            sidebar.style.display = ('none');
            banner.style.display  = 'none';
            body.style.marginTop = ('80px');
        }else{
            sidebar.style.display = ('block');
            banner.style.display = 'block';
            body.style.marginTop = ('220px');

        }
    }

//Side Menu Function 2
var dfault = false   
function get_random_color() {
    var letters = '0123456789ABCDEF'.split('');
    var color = '#';
    for (var i = 0; i < 6; i++ ) {
        color += letters[Math.round(Math.random() * 15)];
    }
    return color;
}
function randFrame(){
    vid = document.getElementById("video_container");
    c = document.getElementById('controls');

    if(!dfault){
        vid.style.backgroundColor  = get_random_color();
        c.style.backgroundColor = vid.style.backgroundColor;
        c.style.backgroundImage = 'none'; 

    }else{
        vid.style.backgroundColor = ('black');
        c.style.backgroundImage = "url('images/bg/bg4.jpg')";
        c.style.backgroundColor = 'none';
    }

    dfault = !dfault
}

//Side Menu Function 3
function randNum(){
    num=Math.floor((Math.random()*13)+1); 
    return num;   
}
function randBG(){
    b = document.getElementsByTagName('body');
    b[0].style.background = "url('images/bg/bg"+randNum()+".jpg') no-repeat center center fixed"
}
Was it helpful?

Solution

You may want to read up on `localstorage'. A good page is at:

http://diveintohtml5.info/storage.html

So, you could create a key/pair, or update whenever there is a click:

So to read it you would do something like:

var mystore = localStorage.getItem("sidebar");

And to set it you can do:

localStorage.setItem("red", mystore);

So, on loading, look for the keys, and if they exist, set the elements, and whenever there is a click create/update the storage.

For some more info you can look at:

http://paperkilledrock.com/2010/05/html5-localstorage-part-one/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top