Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-08-24
14
Medium Priority
?
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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
 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

609 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