Link to home
Start Free TrialLog in
Avatar of ayrezyle
ayrezyle

asked on

Mac&PC compatible JavaScript

Check out this page:
http://gamtu.netscope.co.za/experiments/default.stm

It does exactly what I want on a PC&IE5, but it needs to work under Mac too.

Anyone?
Avatar of ayrezyle
ayrezyle

ASKER

Here is the code I currently use; I need it to work on a Mac too:

<html>

<body onload="javascript:Init()">

<script language="javascript">
// <!--

// note the BODY tag above; the onload event is used to set up the list of image filenames

      var numberOfImages = 5;            // how many images do you have?
      var numberOfSeconds = 2;      // how long between image swaps?
      var rndnum = Math.random();

      var counter = Math.round(numberOfImages * rndnum + 1);
      var imgList = new Array(numberOfImages);      // to store the filenames

// -->
</script>

      
<script language="javascript">
// <!--

function Init() // set up the list of filenames
{
      imgList[1] = "images/form-over.jpg"; // remember to change the first actual image to match this one!
      imgList[2] = "images/gallery-over.jpg";
      imgList[3] = "images/home-over.jpg";
      imgList[4] = "images/pricing-over.jpg";
      imgList[5] = "images/gallery-over.jpg";
      setInterval(nextBanner,numberOfSeconds*1000); // this sets how often "nextBanner" will be called
}

// -->
</script>


<script language="javascript">
// <!--

function nextBanner() // activates the next image in the list (will roll over to the first)
{
      if (counter >= numberOfImages)
      {
            counter = 1;
            document.images.item("banner").src = imgList[counter];
      } else {
            counter++;      
            document.images.item("banner").src = imgList[counter];
      }
}

// -->
</script>

<center><img id="banner" SRC="images/gallery-over.jpg" width="109" height="32"></center>

This page currently swaps images at a 2 second interval.

</body>
</html>
Avatar of Mark Franz
What version of browser on the Mac?  I see nothing wrong with the scripts...
Apparently IE4.5 Does that exist for the Mac? I don't even know, I was never bothered with Mac before. I know the above code runs fine on my PC with Win2k and IE5, but I have no way to test it on a Mac. Any ideas?
Looks like an issue with the scripts, cause they don't work with IE4.7.

Let me do some work on it.
Try using this, putting the scripts in your <head>...</head>

<script language="javascript">
<!--
var numberOfImages = 5; // how many images do you have?
var numberOfSeconds = 2; // how long between image swaps?
var rndnum = Math.random();
var counter = Math.round(numberOfImages * rndnum + 1);
var imgList = new Array(numberOfImages); // to store the filenames

function nextBanner(){
      if (navigator.appName != "Netscape"){
            if (counter >= numberOfImages){
                  counter = 1
                  document.images.item("banner").src = imgList[counter]
            } else {
                  counter++
                  document.images.item("banner").src = imgList[counter]
            }
      } else {
            if (counter >= numberOfImages){
                  counter = 1;
                  document.images['banner'].src = imgList[counter]
            } else {
                  counter++
                  document.images['banner'].src = imgList[counter]
            }
      }
}

function Init(){
      imgList[1] = "images/testimage1.gif";
      imgList[2] = "images/testimage2.gif";
      imgList[3] = "images/testimage3.gif";
      imgList[4] = "images/testimage4.gif";
      imgList[5] = "images/testimage5.gif";
      setInterval("nextBanner()",numberOfSeconds*1000);
}

// -->
</script>


<body onload="JavaScript:Init()">

The setInterval was not called correctly and it needs to call the function, after the function has been declared.  I also added functionality for NS if needed.

davlun
Forgot to mention, I renamed the image in the body of your doc to name="" rather than id="".  This was to accomodate NS.

davlun
OK I made the changes you suggested, but apparently it's still not working. IE5 for the PC handles it just fine though.

Here's the URL again for anyone with a Mac:
http://gamtu.netscope.co.za/experiments/default.stm
Are you getting a particular error message?  Is it a script error or is the page just not functioning?

davlun
It apparently just doesn't do anything.
Well I am not sure then either since I do not have a mac, but I do know it now works in IE4+, you said it works in IE5, works in NS4.7 so I think it is working in most of what is out there.  The only thing I can think of is something I rememeber reading a long time ago about mac's not liking the random function.

I will remove and repost code:
davlun
Try this without the random number.  If it works, you can get a script that will give you a near random number without using the Math.random(), I thought I had one but could not find it.

<html>
<HEAD>
<TITLE>SOME TITLE</TITLE>
 
<script language="javascript">
<!--
var numberOfImages = 5; // how many images do you have?
var numberOfSeconds = 2; // how long between image swaps?
var thisdate = new Date()
var thisdate2 = new Date()
counter= Math.round((thisdate.getTime()/thisdate2.getDate())*.0000000001)
var imgList = new Array(numberOfImages); // to store the filenames

function nextBanner(){
      if (navigator.appName != "Netscape"){
            if (counter >= numberOfImages){
                  counter = 1
                  document.images.item("banner").src = imgList[counter]
            } else {
                  counter++
                  document.images.item("banner").src = imgList[counter]
            }
      } else {
            if (counter >= numberOfImages){
                  counter = 1;
                  document.images['banner'].src = imgList[counter]
            } else {
                  counter++
                  document.images['banner'].src = imgList[counter]
            }
      }
}

function Init(){
      imgList[1] = "images/testimage1.gif";
      imgList[2] = "images/testimage2.gif";
      imgList[3] = "images/testimage3.gif";
      imgList[4] = "images/testimage4.gif";
      imgList[5] = "images/testimage5.gif";
      setInterval("nextBanner()",numberOfSeconds*1000);
}

// -->
</script>

</head>

<body bgcolor="#006666" onload="JavaScript:Init()">
<center>
  <img name="banner" SRC="images/testimage1.gif" width="250" height="175">
</center>
</body>
</html>


davlun
Interesting way of getting a randomish number! I'll hard code the number as well to see if that helps. Time for some more testing...
As for the random part, since you are working with a narrow number of images, many will default to the >= category.  Depending on the day of the month, the numbers can fall between 1 and 12 from my testing but I have never used it where precision counted so I did not test a lot.

davlun
That's fine, I was just teasing. I asked a friend with a Mac to check out that code. I'll assign the points as soon as we know if it works.
ASKER CERTIFIED SOLUTION
Avatar of davlun20080
davlun20080

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No that still wasn't it, but since we're not getting anywhere, nevermind. I'll go to the client and try it from the Mac itself. Then at least I can take it step by step and I won't be working in the dark.

Thanks for the input, you can have the points even though we didn't get to a solution.
SOLUTION! Check out our final solution. It was simply the MAC that was full of BS.

The site is now up & running at www.helderbergcompass.co.za

Thanks for the support!