2

Using jquery, is it possible to swap the whole css file with another css file based on a querystring parameter?

For example, if the url is http://mysite.com/page1.php?style=1

This should use style1.css

If the url is http://mysite.com/page1.php?style=2

This should use style2.css

How can this be done with jquery

14
  • Why the arbitrary jQuery requirement? How do you know that jQuery provides a valid solution to this? Commented May 31, 2011 at 13:25
  • 1
    Since it looks like you are using PHP, I'd recommend detecting the query string there and changing your stylesheet accordingly. Using jQuery, there may be a flash of unstyled content while it loads and changes the CSS files. Commented May 31, 2011 at 13:27
  • if I said, "can this be done with jquery", I would have gotten strange answers like "yes", without out showing how it can be done with jquery. I would prefer not to use raw javascript. Commented May 31, 2011 at 13:28
  • @oshirowanen: FWIW, it's unlikely that any Javascript at all is your best approach. Why not do it in PHP? Commented May 31, 2011 at 13:29
  • Basically, the pages already exist, thousands of them. I don't want to have to go and edit every single page. But at the same time, I need all the old pages to keep the old style sheet. I want the old pages to only use the new style sheet if they are requested from a certain page. Commented May 31, 2011 at 13:30

3 Answers 3

5

This doesn't strictly answer the question posed, but it best solves the problem:

Do it in PHP.

<html>
   <head>
      <?php
      if (!is_numeric(@$_GET['style']))
         die("No style ID given!");

      echo '<link rel="stylesheet" href="style' . $_GET['style'] . '.css" />';
      ?>
   </head>

   <body>
   </body>
</html>
Sign up to request clarification or add additional context in comments.

3 Comments

+1 Though I'd probably do if (!is_numeric(@$_GET['style'])) $style = 0; and use a default stylesheet rather than ending the script because the query parameter didn't exist.
I have updated the question. The previous question did not make sense.
@oshirowanen: I think you should have deleted it and created a new one. This is barely related.
2

link - http://mysite.com/page1.php?style=2

css - <link rel="stylesheet" type="text/css" href="style1.css" />

if ( location.search )
{
   var style = location.search.replace("?","").split("=");
   if ( style[0] === "style" && parseInt( style[1]) === 2 )
   {
      $("link").attr("href","style2.css");
   }
}

Based on your question before the edit. Tested on Chrome

Comments

0

+1 for the server-side solution given by Tomalak

However, if you want to check the query string client side there are possibilities, check out this one for example:

How can I get query string values in JavaScript?

2 Comments

I have updated the question. The previous question did not make sense.
Hehe question makes more/other sense now, but you should consider deleting the current question and re-asking the question you wanted to as: with all the current replies I don't think this thread can be helpful anymore to ppl who stumble to this corner of the interwebz ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.