• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

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

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
tingleweb
Asked:
tingleweb
  • 7
  • 5
  • 2
1 Solution
 
keteracelCommented:
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
 
keteracelCommented:
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
 
hemebondCommented:
<!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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
tinglewebAuthor Commented:
hemebond

Thanks for your response

I get a javascript error though

'document.styleSheets[...].ownerNode' is null or not an object
0
 
hemebondCommented:
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
 
tinglewebAuthor Commented:
I'm not sure what you are suggesting

If I dont use IE, surely most of my users will be using it??
0
 
tinglewebAuthor Commented:
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
 
tinglewebAuthor Commented:
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
 
hemebondCommented:
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
 
tinglewebAuthor Commented:
hemebond

Thank you for your reply

It doesnt throw an error anymore but neither does it rotate through stylesheet on refresh.
0
 
hemebondCommented:
Are you sure? It's working fine for me. What version of IE are you using?
0
 
tinglewebAuthor Commented:
0
 
hemebondCommented:
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
 
tinglewebAuthor Commented:
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 7
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now