Solved

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

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
css issues with IE 11 8 34
Porbably easy CSS question about overlapping and offset images 21 26
Else condition 9 16
If condition on Html with Asp 11 14
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

776 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