Learn HTML step-by-step from A to Z or improve your professional skills.  Home HTML and CSS Tutorials Clothing for a Website

Clothing for a Website


Clothing for a WebsiteSuppose you have your own site with a distinctive design. Some people may like it, while others don’t, however pretty it is (this is a usual thing, beauty is in the eye of the beholder). And it would be good if users had the opportunity to choose the design that pleases them during their visit. We are talking about exactly this: how to give your visitors the opportunity to tailor their experience.

It’s possible to simply make a heap of similar sites with different designs, but this is not what we’re after in any case. You know, the place beneath a site is not rubber; as well, it would be an inordinate amount of work, especially if the site is rather large. I propose a different variant – using CSS, JavaScript and Cookie in conjunction. There’s been a lot written about this beast (those who are for the first time hearing these words should do a little background reading).

For the beginning, we need to write two .css-files (it’s possible to make it bigger too, but we will limit ourselves to two different designs for this article). Let’s open notepad and type in the following code:

body
{background-color:#0093AE; font-family: Verdana; font-size: 10pt;}
a {color:#104EAA; text-decoration:none;}
a:hover {color:#104EAA; text-decoration:underline;}
div.one {background-color:#007BAE; color:#ffffff; border-style:solid;
border-color:#2F4F4F; border-right-width:1px; border-bottom-width:0px;
border-left-width:1px; border-top-width:1px; text-align:left;}
div.two {background-color:#00A3CB; border-style:solid;
border-color:#000000; border-right-width:1px; border-bottom-width:1px;
border-left-width:1px; border-top-width:1px; text-align:left;}

Now let’s save our file under the name style1.css. Then we will do the same with a different file (only this time the name will be style2.css). Here’s the code:

body
{background-color:#F6FFEF; font-family: Verdana; font-size: 10pt; }
a {color:#104EAA; text-decoration:none;}
a:hover {color:#104EAA; text-decoration:underline;}
div.one {background-color:#C8D4Bf; border-style:solid;
border-color:#2F4F4F; border-right-width:1px; border-bottom-width:0px;
border-left-width:1px; border-top-width:1px; text-align:left;}
div.two {background-color:#FFF5D2; border-style:solid;
border-color:#000000; border-right-width:1px; border-bottom-width:1px;
border-left-width:1px; border-top-width:1px; text-align:left;}

Now for a little explanation. We have created two different styles, in other words two different skins for our site. We’ve determined the background color, the font size (10pt), its type (Verdana), and defined the surface appearance tag <div>.

Now that we’ve finished working with the style, let’s move on to script. Using JS (JavaScript), we will write two scripts — one for placing a Cookie, the other reading the Cookie and the installation of new design (style files, of which we have two). By the way, some will possibly have asked the question, Why should we use a Cookie? The thing is, every time a person visits your site, he will not wish to choose his favorite design. It is a bit more comfortable (for your visitors) to make sure that they need choose their favorite skin only once, that it will download every time by default (this can be achieved by using a cookie). Here is the code for the first script:

<script>
function SetCookie(value)
{
    var expdate = new Date ();
    expdate.setTime(expdate.getTime() + (3650 * 24 * 60 * 60 * 1000));
    document.cookie = "mkstyle=" + escape (value) + "; expires=" + expdate.toGMTString() + "; path=/";
}
</script>

Now we will need to create different script. For the sake of comfort, let’s save it as an individual file with the name mycomp.js (.js — this is the standard extension for files containing Java-script). And we will include it in our webpage using this tag <script src=mycomp.js> </script>. So, open notepad, write the following code and save it in the file mycomp.js:

var prefix = "mkstyle=";
var cookieStartIndex = document.cookie.indexOf(prefix);
var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
if (cookieEndIndex == -1)
cookieEndIndex = document.cookie.length;
var value=unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
if ((value !='1') & (value!='2'))
document.write('<LINK REL=STYLESHEET TYPE=text/css HREF=style1.css>');
if (value=='1')
document.write('<LINK REL=STYLESHEET TYPE=text/css HREF=style1.css>');
if (value=='2')
document.write('<LINK REL=STYLESHEET TYPE=text/css HREF=style2.css>');

Let’s pause a bit to discuss this script. Up to the line if((value!=’1’)…), we consider this a Cookie. Further is the code which serves as the framing for the needed style. If a person fails to choose design (in other words if this is his first time on your website), file will style1.css will be activated (the first style). If the value is 1, then the first style will be loaded; if the value is 2 — the second. The line <LINK REL=STYLESHEET TYPE=text/css HREF=style1.css> is the standard tag for turning on the style file. For choosing the style itself we will make two simple links on the page:

<a href=index.html onClick="SetCookie('1');">Style 1</a> <a href=index.html onClick="SetCookie('2');">Style 2</a>

Now we’ll gather all our work into one pile. By now you should have the following files ready:

- two files containing style (style1.css и style2.css);

- one file containing script (mycomp.js);

- the page itself (index.html);

Below you can see an example of the file index.html (the example was taken from one of my sites):

<html>
<head>

<script>
function SetCookie(value)
{
    var expdate = new Date ();
    expdate.setTime(expdate.getTime() + (3650 * 24 * 60 * 60 * 1000));
    document.cookie = "mkstyle=" + escape (value) + "; expires=" + expdate.toGMTString() + "; path=/";
}
</script>
<script src="/img_articles/3873/mycomp.js"></script>
</head>


<table width=185 align=center border=0>
<tr>
<td width=185 valign=top>
<div class=one><small><b>Choose style</b></small></div>
<div class=two><small>
&nbsp;<a href=index.html onClick="SetCookie('1');">Default</a><br>
&nbsp;<a href=index.html onClick="SetCookie('1');">Style 1</a><br>
&nbsp;<a href=index.html onClick="SetCookie('2');">Style 2</a></div>
</td></small>
</tr>
</table>
</html>

Now you can create an entire wardrobe of interchangeable clothing for your site. When doing this, it will be necessary to correct the code a bit.



Author's URL: Alex Salo
Learn HTML step-by-step from A to Z or improve your professional skills. More Tutorials: Featured Materials | Fresh Materials | More HTML Tutorials at Markuptutorials.com

No comments yet...
Add comments to "Clothing for a Website"

Captcha