Google Answers Logo
View Question
 
Q: need to get website to remember background with a cookie or something. ( Answered,   0 Comments )
Question  
Subject: need to get website to remember background with a cookie or something.
Category: Computers > Internet
Asked by: trimidium-ga
List Price: $4.50
Posted: 28 Jan 2003 10:51 PST
Expires: 27 Feb 2003 10:51 PST
Question ID: 149579
if you go to: http://www.thesmartass.info/ , at the bottom of the page
there are a bunch of numbers.  If you click on those numbers it will
swap an external css file which will change the background of the
website.

What i want it to do is somebody clicks that number and it sets a
cookie or something that when they return to the website it remembers
which css file they had clicked on last.  The code to set the css goes
like this:
<a href="#" target="_self"
onclick="document.styleSheets(0).href='/css/css2.css'";>2, </a>.  If
this code needs to be replaced or just modified doesn't matter to me
so long as it achieves my objectives.

If you can answer that and you can tell me how to put that line into
an iframe and have it switch the css file for a different frame
without reloading the website i will give a decent tip.  (example, if
you go to the above listed website and click on customize at the
bottom of the page you will see that clicking the color schemes
changes the page colors but if you click the images at the top it does
as well however it does not effect the non iframe that is below it.)

For reference I use, html, javascript, php, css, sql for my website.

_.,-*~'^^'~*-,.__.,-*~'^

   saagnyL semaJ
www.thesmartass.info

_.,-*~'^^'~*-,.__.,-*~'^

Request for Question Clarification by sycophant-ga on 29 Jan 2003 00:57 PST
Trimidium, 

I can absolutly answer your cookie question (provide code and a brief
tutorial).

However, I am not sure about the iframe thing. I can think of a couple
of ways it should work, based on the Javascript method you are using
at present, however none are, at this point, tested. But, it should be
noted that your CSS does not appear to work in Mozilla, and also the
Javascript change seems a little flawed, although that may just be the
specifics of your stylesheet.

I can, however, provide you with PHP code that will do away with the
JS, and work in Mozilla and IE (and Netscape and Opera, which I
haven't tested your site in) - however they would require reloads to
work.

If you can detail why you are trying to avoid the reload, I maybe able
to suggest some other solutions that a more browser-friendly.

Let me know... 

Regards,
sycophant-ga

Clarification of Question by trimidium-ga on 29 Jan 2003 07:46 PST
simply that it's quicker, and you could click the number when you are
on
say the contact page, and it would remain there and have the new
background without forcing somebody to reload the index/links pages. 
I guess I can live with it reloading to the index if that's the only
way it can do it.  However it the page has to be reloaded then i'm
sure you could make the background changing script work from inside
the iframe somehow.

Please feel free to eliminate any of my javascript for php.  The
higher the compatability rate the better.
Answer  
Subject: Re: need to get website to remember background with a cookie or something.
Answered By: sycophant-ga on 29 Jan 2003 22:52 PST
 
Hi,

Here is a simple-ish PHP implementation that will allow the
cookie-stored CSS theme to be accessed throughout the site, like so:

Put the folling code in a file (I'd call it cookie.php):
--- BEGIN ---
<?php

if (!empty($settheme)) {
	$theme = $settheme;
	setTheme();
} elseif (!empty($_COOKIE['theme'])) {
	$theme = $_COOKIE['theme'];
} elseif (!empty($killcookie)) {
	killCookie();
}


function setTheme() {
	global $theme;
	setcookie('theme',$theme,time()+2678400,'/','thesmartass.info');
}

function getTheme() {
	global $theme;
	
	$default_theme = "css.css";

	$css_dir = "./css/";	// trailing slash is important.
	$css_path = "cas/";	// here too.
	$css_prefix = "css";
	$css_suffix = ".css";

	if (!empty($theme)) {
		$cssfile = $css_prefix.$theme.$css_suffix;
		if (!file_exists($css_dir.$cssfile)) {
			$cssfile = $default_theme;
		}
	} else {
		$cssfile = $default_theme;
	}
	
	return $css_path.$cssfile;
}

function killCookie() {
	setcookie('theme','',time()-2678400);
}

?>

--- END ---


Now, at the top of each page (which needs to be php passed) before any
HTML is output, you should add:
<?php include("cookie.php"); ?>

Replace your CSS link with:
<link rel="stylesheet" type="text/css" href="<?php echo getTheme();
?>">

And the CSS links should be:
<a href="<?php echo $PHP_SELF; ?>?settheme=1">1</a> (etc...)

And if you'd like to create a link to clear theme cookies, add
something like:
<a href="<?php echo $PHP_SELF; ?>?killcookie=1">Clear Themes</a>

Try it out and let me know if you have any feedback about it, as there
are a number of minor changes that can be made to alter it's
operation.

As for the reloading the contents of the frame, the easiest way to do
that, is rather than just loading the contents of a frame using the
traditional 'target=' method, write a simple handler:
<?php
    if (empty($page)) {
        $page = "links"
    }
?>
<iframe [...] src="/<?php echo $page; ?>.php"></iframe>

This way, your News link changes to: 
<a href="index.php?page=news">News</a>

It means the main page has to reload, but it also makes the content of
that page linkable within the frameset.

Furthermore, you can add a Javascript to force the page to be loaded
in the frameset if it is loaded on it's own. This is especially handy
when you consider that your content may well be indexed on search
engines without the frameset context.

I hope this helps, let me know if you have any problems. I haven't run
this code specifically, but have written similar code many times in
the past, and that code did pass a basic syntax checker.

Regards,
sycophant-ga
Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy