Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Change my forum background with Javascript: cookies question
Old 01-19-2009, 01:17 PM Change my forum background with Javascript: cookies question
Dakoom's Avatar
Experienced Talker

Posts: 42
Name: Norman
Location: [Italy]
Trades: 0
Hello there, I would like to give my user's the ability to change the background of my forum. I have this .js script:

Code:
function change_it() {
    
  if (document.getElementById && document.createTextNode) {
      
    var new_color = "";
    var color_sel = document.getElementById("change_bg").value;
    color_sel *= 1;
    
    switch (color_sel) {
      case 0 : window.alert("Per favore seleziona un colore."); return false;
      case 1 : new_color = "#FFFF00"; break;
      case 2 : new_color = "#3333FF"; break; 
      case 3 : new_color = "#009900"; break;
    }
    
    document.body.style.backgroundColor= new_color;
    return false;
    
  }
}
And this HTML code:

HTML Code:
<!-- Bg Color Switcher -->
<form action="#" onsubmit="return change_it();">
<select name="change_bg" id="change_bg">
<option selected="selected" value="0">Color:</option>
<option value="1">Yellow</option>
<option value="2">Blue</option>
<option value="3">Green</option>
</select> 
<input type="submit" value="Cambia!" />
</form>
<script type="text/javascript" src="clientscript/bgcolor_switcher.js"></script>
<!-- ### -->
And it works.. but just for the page they are browsing. When they click on another link, they enter a thread or something else, the background return the default one. So, my question is.. is there a way I can set a cookies reminder in order to save the selected backgorund for 30 days, for example.. ? Thank you.
__________________
I'm italian, I am learning english.. so, please, if you see any errors in my grammar or spelling, let me know via PM. Danke!

Please login or register to view this content. Registration is FREE

Last edited by Dakoom; 01-19-2009 at 01:20 PM..
Dakoom is offline
Reply With Quote
View Public Profile Visit Dakoom's homepage!
 
 
Register now for full access!
Old 01-19-2009, 06:27 PM Re: Change my forum background with Javascript: cookies question
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,520
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
http://www.elated.com/articles/javascript-and-cookies/
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-20-2009, 10:03 AM Re: Change my forum background with Javascript: cookies question
Dakoom's Avatar
Experienced Talker

Posts: 42
Name: Norman
Location: [Italy]
Trades: 0
I can't understand how to make it to work. I have tried this:

Code:
function writeCookie( )
{
        var name = document.change_bg.selection.value;
        document.cookie = "coloreBg=" + name;
}

function change_it() {
    
  if (document.getElementById && document.createTextNode) {
      
    var new_color = "";
    var color_sel = document.getElementById("change_bg").value;
    color_sel *= 1;
    
    switch (color_sel) {
      case 0 : window.alert("Per favore seleziona un colore."); return false;
      case 1 : new_color = "#FFFF00"; break;
      case 2 : new_color = "#3333FF"; break; 
      case 3 : new_color = "#009900"; break;
    }
    
    document.body.style.backgroundColor= new_color;
    return false;
    
  }
}

function readCookie( )
{
        if (document.cookie != "") //make sure the cookie exists
        {
                var coloreBg = document.cookie.split("=")[1]; //Get the value from the name=value pair
                document.write ("Hello, " + coloreBg);
        }
}
.. with this HTML code:

HTML Code:
<!-- Bg Color Switcher -->
<form action="#" name="change_bg" onsubmit="return change_it();" onClick="setCookie('bgcolor',''selection, +30)">
<select name="change_bg" id="change_bg">
<option selected="selected" value="0">Seleziona un Colore</option>
<option value="1">Yellow</option>
<option value="2">Blue</option>
<option value="3">Green</option>
</select> 
<input type="submit" name="selection" value="Cambia!" />
</form>
<script type="text/javascript" src="clientscript/bgcolor_switcher.js"></script>
<!-- ### -->
But nothing. Can you please help me?
__________________
I'm italian, I am learning english.. so, please, if you see any errors in my grammar or spelling, let me know via PM. Danke!

Please login or register to view this content. Registration is FREE
Dakoom is offline
Reply With Quote
View Public Profile Visit Dakoom's homepage!
 
Old 01-20-2009, 08:21 PM Re: Change my forum background with Javascript: cookies question
stevej's Avatar
Professional Multitasker

Posts: 996
Location: Not positive
Trades: 0
Well if I were you, I would do it with php. Less code. And, in my view, much simpler. Here is the code. Put this on any page where you want the dynamic background applied:
PHP Code:
<?php 
if($_SESSION['color'] == '#FFFF00') { //if it's yellow
    
echo "<style type=\"text/css\">body {background: #FFFF00;}</style>";
}
if(
$_SESSION['color'] == '#3333FF') { //if it's blue
    
echo "<style type=\"text/css\">body {background: #3333FF;}</style>";
}
if(
$_SESSION['color'] == '#009900') { //if it's green
    
echo "<style type=\"text/css\">body {background: #009900;}</style>";
}
if( empty(
$_SESSION) ) { //if there is no set background
    //nothing here!
}
?>
Put this on the page where the user sets their background color: (along with the code above, of course)
PHP Code:
<?php
$color 
$_POST['change_bg'];
if(
$_POST['submit']){
    echo 
"<style type=\"text/css\">body {background: ".$color.";}</style> Per favore seleziona un colore.";
    
$_SESSION['color'] = $color;
}
?>
<form method="post">
<select name="change_bg">
<option selected="selected" value="#FFFFFF">None</option>
<option value="#FFFF00">Yellow</option>
<option value="#3333FF">Blue</option>
<option value="#009900">Green</option>
</select> 
<input type="submit" value="Cambia!" name="submit" />
</form>
And in every page you want it to work, put the code
PHP Code:
<?php session_start(); ?>
at the top before any code. If you're using a premade forum, it may already have that.

Ask if you have any questions!
- Steve
stevej is offline
Reply With Quote
View Public Profile
 
Old 01-21-2009, 09:03 AM Re: Change my forum background with Javascript: cookies question
Dakoom's Avatar
Experienced Talker

Posts: 42
Name: Norman
Location: [Italy]
Trades: 0
Thank you very much, stevej. In my vBulletin forum I have added the PHP code on a plugin:

PHP Code:
session_start();  

if(
$_SESSION['color'] == '#FFFF00') { //if it's yellow 
    
echo "<style type=\"text/css\">body {background: #FFFF00;}</style>"

if(
$_SESSION['color'] == '#3333FF') { //if it's blue 
    
echo "<style type=\"text/css\">body {background: #3333FF;}</style>"

if(
$_SESSION['color'] == '#009900') { //if it's green 
    
echo "<style type=\"text/css\">body {background: #009900;}</style>"

if( empty(
$_SESSION) ) { //if there is no set background 
    //nothing here! 


$color $_POST['change_bg']; 
if(
$_POST['submit']){ 
    echo 
"<style type=\"text/css\">body {background: ".$color.";}</style>"
    
$_SESSION['color'] = $color

And in the header template I have added this:

HTML Code:
<form method="post">
<select name="change_bg">
<option selected="selected" value="#FFFFFF">None</option>
<option value="#FFFF00">Yellow</option>
<option value="#3333FF">Blue</option>
<option value="#009900">Green</option>
</select> 
<input type="submit" value="Cambia!" name="submit" />
</form>
But.. it doesn't work. When I select a colour and I click "Change".. it reload the page but it is still the same. Here you can see yourself:

http://community.dakoom.com/index.php
__________________
I'm italian, I am learning english.. so, please, if you see any errors in my grammar or spelling, let me know via PM. Danke!

Please login or register to view this content. Registration is FREE
Dakoom is offline
Reply With Quote
View Public Profile Visit Dakoom's homepage!
 
Old 01-21-2009, 01:10 PM Re: Change my forum background with Javascript: cookies question
stevej's Avatar
Professional Multitasker

Posts: 996
Location: Not positive
Trades: 0
Ok... If I were you, I would do this:
Put this code in the very very top, before any html, even before the doctype declaration. Probably in the header file. (You are using includes on the forum theme, right?)
PHP Code:
 <?php session_start(); ?>
Then make a plugin with this next code I'll give you: doesn't matter where, but you should put it after you link to the external style sheet.
PHP Code:
<?php
if($_SESSION['color'] == '#FFFF00') { //if it's yellow 
    
echo "<style type=\"text/css\">body {background: #FFFF00;}</style>"

if(
$_SESSION['color'] == '#3333FF') { //if it's blue 
    
echo "<style type=\"text/css\">body {background: #3333FF;}</style>"

if(
$_SESSION['color'] == '#009900') { //if it's green 
    
echo "<style type=\"text/css\">body {background: #009900;}</style>"

if( empty(
$_SESSION) ) { //if there is no set background 
    //nothing here! 

?>
Once you've done that, you have established the whole cookie system. This next code is the form code. It too, should come after the link to the external stylesheet. And it should go together. Just for the sake of simplicity.
PHP Code:
<?php
$color 
$_POST['change_bg'];
if(
$_POST['submit']){
    echo 
"<style type=\"text/css\">body {background: ".$color.";}</style> Per favore seleziona un colore.";
    
$_SESSION['color'] = $color;
}
?>
<form method="post">
<select name="change_bg">
<option selected="selected" value="#FFFFFF">None</option>
<option value="#FFFF00">Yellow</option>
<option value="#3333FF">Blue</option>
<option value="#009900">Green</option>
</select> 
<input type="submit" value="Cambia!" name="submit" />
</form>
Hopefully that will work. You probably aren't at liberty to do this, but it would probably help if I could see the whole unrendered php page.

- Steve

Last edited by stevej; 01-21-2009 at 01:11 PM..
stevej is offline
Reply With Quote
View Public Profile
 
Old 01-21-2009, 02:16 PM Re: Change my forum background with Javascript: cookies question
Dakoom's Avatar
Experienced Talker

Posts: 42
Name: Norman
Location: [Italy]
Trades: 0
My problem is that on vBulletin I can't use PHP code on templates, so I can: 1) include them via .php files, 2) include them as plugins. This for the form part wich includes that part of php code.
__________________
I'm italian, I am learning english.. so, please, if you see any errors in my grammar or spelling, let me know via PM. Danke!

Please login or register to view this content. Registration is FREE
Dakoom is offline
Reply With Quote
View Public Profile Visit Dakoom's homepage!
 
Old 01-21-2009, 02:28 PM Re: Change my forum background with Javascript: cookies question
stevej's Avatar
Professional Multitasker

Posts: 996
Location: Not positive
Trades: 0
Ok then, you may have to split it into two plugins... one with the session_start function put at the top before any code, and one with the rest of the code put in after the style information has been given. That is very important. I haven't ever used vBulletin before, so I'm sorry if so if my advice is a no-brainer.

- Steve
stevej is offline
Reply With Quote
View Public Profile
 
Old 01-21-2009, 03:10 PM Re: Change my forum background with Javascript: cookies question
Dakoom's Avatar
Experienced Talker

Posts: 42
Name: Norman
Location: [Italy]
Trades: 0
I will try. Really thanks stevej, again.
__________________
I'm italian, I am learning english.. so, please, if you see any errors in my grammar or spelling, let me know via PM. Danke!

Please login or register to view this content. Registration is FREE
Dakoom is offline
Reply With Quote
View Public Profile Visit Dakoom's homepage!
 
Old 01-22-2009, 12:04 PM Re: Change my forum background with Javascript: cookies question
Dakoom's Avatar
Experienced Talker

Posts: 42
Name: Norman
Location: [Italy]
Trades: 0
Now it works! Just one problem: when the browser is closed, the cookies gone away. And would be great to let them remain for.. mhh, 30 days? Or something like this.

Oh, another question. Do you think is possible to put there a script that open a colours panel, so users can select a really personalized colour and not the ones I have chose?
__________________
I'm italian, I am learning english.. so, please, if you see any errors in my grammar or spelling, let me know via PM. Danke!

Please login or register to view this content. Registration is FREE
Dakoom is offline
Reply With Quote
View Public Profile Visit Dakoom's homepage!
 
Old 01-22-2009, 01:44 PM Re: Change my forum background with Javascript: cookies question
stevej's Avatar
Professional Multitasker

Posts: 996
Location: Not positive
Trades: 0
Doh! I'm such an idiot... Turns out I've been giving you a code that uses sessions this whole time. Not cookies. Oh well, I now have you a new code. It uses cookies, so that they will not expire if the user closes the browser. And, you don't have to use that annoying session_start script at the top. You can get rid of it now. I haven't implemented a color rack, but have made it so that you don't need to start a whole new 'if' function whenever you want to add a new color. So with that being said, here is the code to replace the code that decides whether the user has a cookie or not:
PHP Code:
<?php
if(isset($_COOKIE['color'])) { //if user has cookie
    
$setcolor $_COOKIE['color']; 
    echo 
"<style type=\"text/css\">body {background:".$setcolor.";}</style>"

else {
    
//do nothing.
}
?>
Fortunately, you do not have to change any of the html form, or anything like that. Just some of the php that goes with the form. Change your old form php to this:
PHP Code:
<?php
$color 
$_POST['change_bg']; //takes the values of the options
if($_POST['submit']){ //if our button is clicked
    
echo "<style type=\"text/css\">body {background: ".$color.";}</style> Per favore seleziona un colore.";
    
setcookie("color"$color); //set the cookie
}
?>
And I'm pretty sure you'll be good to go. I'll try to figure out a color rack, though know that will be very complicated.

- Steve

Last edited by stevej; 01-22-2009 at 01:45 PM..
stevej is offline
Reply With Quote
View Public Profile
 
Old 01-22-2009, 08:09 PM Re: Change my forum background with Javascript: cookies question
Dakoom's Avatar
Experienced Talker

Posts: 42
Name: Norman
Location: [Italy]
Trades: 0
Mhh.. now I am including these files:

bgcolor_selection.php:
PHP Code:
<?php
if($_SESSION['color'] == '#000000') { //nero 
    
echo "<style type=\"text/css\">body {background: #000000;}</style>"

if(
$_SESSION['color'] == '#FF0000') { //rosso
    
echo "<style type=\"text/css\">body {background: #FF0000;}</style>"

if(
$_SESSION['color'] == '#00FF00') { //verde
    
echo "<style type=\"text/css\">body {background: #00FF00;}</style>"

if(
$_SESSION['color'] == '#0000FF') { //blu
    
echo "<style type=\"text/css\">body {background: #0000FF;}</style>"

if(
$_SESSION['color'] == '#FFFF00') { //giallo
    
echo "<style type=\"text/css\">body {background: #FFFF00;}</style>"

if(
$_SESSION['color'] == '#00FFFF') { //azzurro
    
echo "<style type=\"text/css\">body {background: #00FFFF;}</style>"

if(
$_SESSION['color'] == '#800080') { //viola
    
echo "<style type=\"text/css\">body {background: #800080;}</style>"

if(
$_SESSION['color'] == '#C0C0C0') { //grigio
    
echo "<style type=\"text/css\">body {background: #C0C0C0;}</style>"

if(
$_SESSION['color'] == '#333333') { //grigio scuro
    
echo "<style type=\"text/css\">body {background: #333333;}</style>"

if( empty(
$_SESSION) ) { //if there is no set background 
    //nothing here! 

?>
session_start.php:
Code:
   <?php session_start(); ?>
before_form.php:
PHP Code:
<?php
$color 
$_POST['change_bg'];
if(
$_POST['submit']){
    echo 
"<style type=\"text/css\">body {background: ".$color.";}</style>";
    
$_SESSION['color'] = $color;
}
?>
Then.. I change my before_form.php file with the code you have provided in the second box. And I'll delete the session_start.php. But.. where do I put the first code you have written?

And, how would be more simple for me to add more colours?
__________________
I'm italian, I am learning english.. so, please, if you see any errors in my grammar or spelling, let me know via PM. Danke!

Please login or register to view this content. Registration is FREE

Last edited by Dakoom; 01-22-2009 at 08:11 PM..
Dakoom is offline
Reply With Quote
View Public Profile Visit Dakoom's homepage!
 
Old 01-22-2009, 08:51 PM Re: Change my forum background with Javascript: cookies question
stevej's Avatar
Professional Multitasker

Posts: 996
Location: Not positive
Trades: 0
You replace bgcolor_selection.php with this:
PHP Code:
 <?php
if(isset($_COOKIE['color'])) { //if user has cookie
    
$setcolor $_COOKIE['color']; 
    echo 
"<style type=\"text/css\">body {background:".$setcolor.";}</style>"

else {
    
//do nothing.
}
?>
And will work no matter how many different colors you want to have! It's completely dynamic.

- Steve
stevej is offline
Reply With Quote
View Public Profile
 
Old 02-05-2009, 11:21 AM Re: Change my forum background with Javascript: cookies question
Dakoom's Avatar
Experienced Talker

Posts: 42
Name: Norman
Location: [Italy]
Trades: 0
Thank you Steve!
__________________
I'm italian, I am learning english.. so, please, if you see any errors in my grammar or spelling, let me know via PM. Danke!

Please login or register to view this content. Registration is FREE
Dakoom is offline
Reply With Quote
View Public Profile Visit Dakoom's homepage!
 
Old 03-04-2009, 07:53 AM Re: Change my forum background with Javascript: cookies question
Junior Talker

Posts: 1
Trades: 0
Love this code, just what I needed help with, is there any way someone could tell me how to set it up using image links to change the bg colour instead of drop down text links in a form?

Any help would be much appreaciated
lailag is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Change my forum background with Javascript: cookies question
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.93639 seconds with 12 queries