Solved

Why do I love all of my javascript changes once a form is submitted on my webpage?

Posted on 2012-03-14
5
183 Views
Last Modified: 2012-03-14
My webpage below has a changeable background color, but once the form is submitted or once the page is reloaded, it goes back to the default color. Is there a simple way to prevent that in javascript?

my webpage
0
Comment
Question by:shampouya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 37722858
I presume that when you say "love", you mean "save" ;-)
0
 
LVL 15

Expert Comment

by:StingRaY
ID: 37723189
You may save your background color into cookie and load them when the page is loaded.
0
 

Author Comment

by:shampouya
ID: 37723417
Sorry I meant to say why do I lose all my javascript changes. Is there any way to keep the changes once the form on my page is submitted? Does the form being submitted count as a reload of the page?
0
 
LVL 15

Accepted Solution

by:
StingRaY earned 500 total points
ID: 37723436
For example:

Add these functions to your script:

function saveTheme(theme)
{
    setCookie('theme', theme);
}

function loadTheme()
{
    theme = getCookie('theme');
    if (theme == 'purple') { purpleTheme(); }
    else { if (theme == 'blue') { blueTheme(); }
    else { if (theme == 'red') { redTheme(); } }
    }
}

function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

Open in new window


and have some mods to your html

<li><img src="purpleSquare.jpg" onClick="purpleTheme();saveTheme('purple');" id="purpleSquare"></li>
<li><img src="blueSquare.jpg" onClick="blueTheme();saveTheme('blue');" id="blueSquare"></li>
<li><img src="redSquare.jpg" onClick="redTheme();saveTheme('red');" id="redSquare"></li>

Open in new window


and also put this to your script file
$(function(){
    loadTheme();
});

Open in new window

0
 

Author Closing Comment

by:shampouya
ID: 37723480
thanks
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question