Solved

How get image to rotate through a folder of images or rotate through stylesheets?

Posted on 2004-08-24
14
218 Views
Last Modified: 2006-11-17
On http://www.nationaltrust.org.uk/main/ - it changes the image on the page and background colour on refresh.  Its almost as though the css changes on refresh.

How?

Is this possible if the images to be rotated are in a folder and the css just rotates through the folder on every refresh or every 10 minutes?

Any ideas always welcome :@)
0
Comment
Question by:tingleweb
  • 7
  • 5
  • 2
14 Comments
 
LVL 9

Expert Comment

by:keteracel
ID: 11880971
you'll need to set a cookie each time the page is loaded and then just change the stylesheet depending on the number you're currently on...

so, in the head you'll need to load all the stylesheets (NOTE: the last is the one used by the browser to render the page):

...
<head>
<link rel="stylesheet" href="style_1.css">
<link rel="stylesheet" href="style_2.css">
<link rel="stylesheet" href="style_3.css">
<link rel="stylesheet" href="style_4.css">
<link rel="stylesheet" href="style_1.css">
...

then you'll need the part to check if the cookie already exists:

...
<script type="text/javascript">
function readCookie(name) {
        var nameEQ = name + "=";
      var ca = document.cookie.split(';');
      for(var i=0;i < ca.length;i++)
      {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
      }
      return null;
} //from quirksmode.org

function writeCookie(name,value,days)
{
      if (days)
      {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
      }
      else var expires = "";
      document.cookie = name+"="+value+expires+"; path=/";
}

var current = readCookie("ss");

if (current) {
        current++;
         if (current > 4) {
                 current = 1;
         }
}
else {
        current = 1;
}

writeCookie("ss", current, 10);
   
for(var i=0;i < document.styleSheets.length;i++){
      if(i!=current){
            document.styleSheets[i].disabled=true;
      }else{
            document.styleSheets[i].disabled=false;
      }
 }
</script>
</head>

that should do....
0
 
LVL 9

Expert Comment

by:keteracel
ID: 11882184
the for loop is slightly wrong, should be:

for(var i=0;i < document.styleSheets.length;i++){
      if(i!=current-1){
            document.styleSheets[i].disabled=true;
      }else{
            document.styleSheets[i].disabled=false;
      }
 }
0
 
LVL 3

Accepted Solution

by:
hemebond earned 500 total points
ID: 12186834
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
      <head>
            <title>Q_21105088</title>
            <link rel="alternate stylesheet" type="text/css" href="/css/red.css" title="red">
            <link rel="alternate stylesheet" type="text/css" href="/css/blue.css" title="blue">
            <link rel="alternate stylesheet" type="text/css" href="/css/green.css" title="green">
      </head>
      <body>
      </body>
      <script type="text/javascript">
            var list = new Array();
            for(var i = 0; i < document.styleSheets.length; i++)
            {
                  if(document.styleSheets[i].disabled == true && document.styleSheets[i].ownerNode.getAttribute("rel") == "alternate stylesheet")
                  {
                        list.push(document.styleSheets[i]);
                  }
            }
            if(list.length)
            {
                  list[Math.round(Math.random() * (list.length - 1))].disabled = false;
            }
      </script>
</html>
0
 

Author Comment

by:tingleweb
ID: 12227740
hemebond

Thanks for your response

I get a javascript error though

'document.styleSheets[...].ownerNode' is null or not an object
0
 
LVL 3

Expert Comment

by:hemebond
ID: 12231584
Well, it's in the spec, so I guess you're using Internet Explorer. In which case I can't really help you. You could remove that check, but then you're unable to differentiate between alternate stylesheets and other stylesheets. My suggestion, as always, is to ignore IE when it comes to advanced stuff. As long as it degrades properly for them, there's not really a problem.

http://www.w3.org/TR/DOM-Level-2-Style/stylesheets
0
 

Author Comment

by:tingleweb
ID: 12238093
I'm not sure what you are suggesting

If I dont use IE, surely most of my users will be using it??
0
 

Author Comment

by:tingleweb
ID: 12238133
Would this work?

<script type="text/javascript">
          var list = new Array();
          for(var i = 0; i < document.styleSheets.length; i++)
         // {
               //if(document.styleSheets[i].disabled == true && document.styleSheets[i].ownerNode.getAttribute("rel") == "alternate stylesheet")
               //{
                    //list.push(document.styleSheets[i]);
               //}
          //}
          if(list.length)
          {
               list[Math.round(Math.random() * (list.length - 1))].disabled = false;
          }
     </script>
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:tingleweb
ID: 12238465
keteracel

I have tried your code too.
It doesnt loop through the stylesheets tho.

____________________________________________________________________________

<link rel="stylesheet" href="/css/bf2.css">
<link rel="stylesheet" href="/css/bf3.css">
<link rel="stylesheet" href="/css/bf4.css">
<link rel="stylesheet" href="/css/bf2.css">


<script type="text/javascript">
function readCookie(name) {
        var nameEQ = name + "=";
     var ca = document.cookie.split(';');
     for(var i=0;i < ca.length;i++)
     {
          var c = ca[i];
          while (c.charAt(0)==' ') c = c.substring(1,c.length);
          if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
     }
     return null;
} //from quirksmode.org

function writeCookie(name,value,days)
{
     if (days)
     {
          var date = new Date();
          date.setTime(date.getTime()+(days*24*60*60*1000));
          var expires = "; expires="+date.toGMTString();
     }
     else var expires = "";
     document.cookie = name+"="+value+expires+"; path=/";
}

var current = readCookie("ss");

if (current) {
        current++;
         if (current > 4) {
                 current = 1;
         }
}
else {
        current = 1;
}

writeCookie("ss", current, 10);
   
for(var i=0;i < document.styleSheets.length;i++){
      if(i!=current-1){
            document.styleSheets[i].disabled=true;
      }else{
            document.styleSheets[i].disabled=false;
      }
 }
</script>
0
 
LVL 3

Expert Comment

by:hemebond
ID: 12242889
Change:

if(document.styleSheets[i].disabled == true && document.styleSheets[i].ownerNode.getAttribute("rel") == "alternate stylesheet")

to:

if(document.styleSheets[i].disabled == true)

to support Internet Explorer. Keep in mind though, that you may end up disabling a stylesheet you want, by removing that "alternate stylesheet" check.

This code (all of it) doesn't work in Opera. Something Opera doesn't support.
0
 

Author Comment

by:tingleweb
ID: 12246445
hemebond

Thank you for your reply

It doesnt throw an error anymore but neither does it rotate through stylesheet on refresh.
0
 
LVL 3

Expert Comment

by:hemebond
ID: 12246574
Are you sure? It's working fine for me. What version of IE are you using?
0
 

Author Comment

by:tingleweb
ID: 12246597
0
 
LVL 3

Expert Comment

by:hemebond
ID: 12253479
Okay, first of all you have a <link> element outside your head element. Not only that, but you also have it after your other stylesheets, so it will overwrite any settings in the others. They are cascading stylesheets. This means stylesheets loaded later, will over-lay the previous stylesheets.

Move it back inside your head element, before your alternate stylesheets.
0
 

Author Comment

by:tingleweb
ID: 12256442
Genius hemebond - what can I say!

"Speech..speech!!"

"I'd like to thank hemebond for his support and answers, my computer..and experts exchange..<sob sob>...."
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now