Solved

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

Posted on 2012-03-14
5
178 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
This article discusses how to create an extensible mechanism for linked drop downs.
In this Micro Tutorial viewers will learn how to create a CSS image sprite (In a later tutorial, viewers will learn how to use CSS and HTML to create a navigation menu using this sprite) Open a new Photoshop document with a width of (Icon width)x(N…
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now