views:

52

answers:

2

To apply a CSS to an existing html page I need to add a link that links to the css file, I am asked to include a link in the webpage that I am building that would link to the same html page but with a different css file, I am thinking I need to create a different css file, then create another .html page by copy the exact content from the first page and only change the link of the css file, but it doesn't seem so efficient and I am assuming there should be a standard method to do this.

thanks

A: 

you can use a JAVAscript to documet.write a link

Anon
A: 

If you can use a server-side language like PHP, I would do it something like this:

<?php

   $allowed_stylesheets = array("red", "white", "blue", "green", "mobile");
   $default_stylesheet = "red";

   $stylesheet = 
   (in_array($_GET["stylesheet"]) ? $_GET["stylesheet"] : $default_stylesheet);

?>

<link rel="stylesheet" type="text/css" href="<?php echo $stylesheet; ?>">

you would then be able to call your page, and switch the style sheet like so:

www.example.com/page.php?stylesheet=mobile

note that to make a .html page run in PHP, there is probably some server setup necessary, by default only .php pages are parsed in PHP. Depending on your situation, that may be too much hassle than it's worth. However, I don't know any pure HTML way of switching style sheets reliably.

Also, this way, you will have to repeat the style sheet command every time you call the page.

Pekka
Noona
@Noona I suppose you could do this using Javascript but I would regard that a sub-optimal solution because it wouldn't work for people with Javascript turned off. I can not think of a pure HTML solution that does not require your having two different HTML files. I would add the requirement of no server-side languages to the question as an update, maybe somebody comes up with an idea I haven't thought of.
Pekka
JKirchartz