ASP, PHP and JavaScript switch CSS skin code based on time slots

Source: Internet
Author: User
Tags php and

A website can switch different CSS styles you should all understand, like the well-known Tencent in this year's revision also increased the function of switching skin. According to the time to automatically adjust the site style is a good idea, the time to automatically adjust the site style is not new, remember a long time ago have seen similar articles or methods, but did not pay special attention. Here are a few ways to implement them, and there are two ways to implement them online:

First, the use of service-side code

ASP version:

<link rel="stylesheet" type="text/css" href="
<%
  if hour(now)<12 then
     response.write "morning.css"
  elseif hour(now)<17 then
    response.write "day.css"
   else
    response.write "night.css"
   end if
%>
"/>

PHP Version:

<link rel="stylesheet" type="text/css" href="
<?php
  $hour = date(”H”);
  if($hour < 12)
    echo ‘morning.css’;
  else if($hour < 17)
    echo ‘day.css’;
  else
    echo ‘night.css’;
?>
" />

Second, the use of JavaScript code

<script type="text/javascript">
<!–
  function getCSS(){
    datetoday = new Date();
    timenow=datetoday.getTime();
    datetoday.setTime(timenow);
    thehour = datetoday.getHours();
    if (thehour<12)
      display = "morning.css";
    else if (thehour<17)
      display = "day.css";
    else
      display = "night.css";
    //(…想要更多再加即可…)
    var css = ‘<’;
    css+=’link rel="stylesheet" href=’+display+’\/’;
    css+=’>’;
    document.write(css);
  }
–>
</script>

Given that the client may not support or disallow JavaScript, you need to set a default CSS.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.