开发者

Changing CSS based on querystring

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


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>


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


+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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜