View Full Version : Help with personalised background
thelegendray
07-21-2007, 10:16 AM
Im a newbie at javascript, and needs help in coding my new web.
What i need now is a code when loaded will look for a cookie, if there is a value in the cookie, it will load the value as the background image, if there is no value/no cookie exist, it will create 1 cookie with a default value of an image url.
And at the end of the page there is a form that allow user to input image url for the cookie to remember.
Can someone please generate the code for me, you help is most appreciated.
djr33
07-21-2007, 11:37 AM
Well, you could do it in PHP as well, and it would be pretty easy.
Here's the code for that:
Make sure your page ends with the .php extension and PHP is installed/enabled on your server, then place this at the very top of yourpage:
<?php
if (isset($_POST['bgimage'])) {
$bgimage = $_POST['bgimage'];
setcookie('bgimage',$bgimage,time()+60*60*24*365);
}
else if (isset($_COOKIE['bgimage'])) {
$bgimage = $_COOKIE['bgimage'];
}
else {
$bgimage = 'mydefault.jpg'; //set default here
setcookie('bgimage',$bgimage,time()+60*60*24*365);
}
?>
I set the cookie to expire in one year. Change that if you'd like...
Now, you have the value $bgimage ready for use in setting the background on the page.
Use the following line:
<?=$bgimage?>wherever you want to have the value of the background set.
For example:
<body background="<?=$bgimage?>">
Or, using CSS:
background-image: <?=$bgimage?>;
Etc.
Now, for the form, use some version of this:
<form .... action="">
<input type="text" name="bgimage" value="<?=$bgimage?>">
<input type="submit" value="Change Background">
</form>
A server-side script is definitely the preferred way to do it.
Or, using CSS:
background-image: <?=$bgimage?>;
background-image: url(<?php echo $bgimage; ?>);djr33, never use shorttags when designing code to run on someone else's server. There's no guarantee they'll have them enabled.
djr33
07-21-2007, 02:08 PM
Oops, on the CSS. I was writing it quickly.
As for short tags, isn't it fairly well supported for the simple output format, when just displaying a certain value? I never use anything aside from <?php ... ?> in general, but for displaying a value, I find <?=$val?> or <?=val()?> to be a lot cleaner.
I should have noted, though, that the proper substitute if that fails is to use echo, as in your post.
isn't it fairly well supported for the simple output format, when just displaying a certain value?No. Shorttags are shorttags; there's only one option to enable all of <?, <?=, &c. In my experience, about one in four servers have it disabled.
I should have noted, though, that the proper substitute if that fails is to use echo, as in your post.But that's a lot more effort than just using the full form in the first place :)
thelegendray
07-23-2007, 11:15 PM
I have managed to figure out the code somehow. thanks anyway guys.
thelegendray
07-23-2007, 11:18 PM
but the i will try the php, it might have a better result cause my code is messed up
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.