Results 1 to 9 of 9

Thread: Error in my code ? Help !!

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 1999
    Location
    London, England
    Posts
    213
    I am using the following Javascript to display a banner. It works ok in IE 5 but not in Netscape ( I am using version 4.06).

    How can I get it to work in NS. OR can someone show me some code that will work in both browsers ?

    <HTML>
    <HEAD>
    <TITLE>Javascript to Rotate the Banners</TITLE>
    <SCRIPT LANGUAGE="JavaScript1.2">

    var NS4 = (document.layers) ? 1 : 0;
    var IE4 = (document.all) ? 1 : 0;

    onload = start;

    var ar = new Array();

    ar[0] = "<A><IMG src='img1.jpg' width=400 height=40</A>";
    ar[1] = "<A><IMG src='img2.jpg' width=400 height=40></A>";

    var num = 0;

    function start() {
    setInterval("update()", 1500);
    }

    function update() {
    display("banner", ar[num]);
    num++;
    if (num == ar.length) num = 0;
    }
    function display(id, str) {
    if (NS4) {
    with (document[id].document) {
    open();
    write(str);
    close();
    } } else {
    document.all[id].innerHTML = str;
    }
    }// -->
    </SCRIPT>
    </HEAD>
    <BODY>

    <SPAN ID="banner" STYLE="position: center;"></SPAN>

    </BODY>
    </HTML>

  2. #2
    Lively Member
    Join Date
    Mar 2000
    Location
    Lowestoft
    Posts
    91
    Hi,

    I wouldnt have written it like that, i would have had an image tag in the html, hard coded in, and then update the image source, I think that would be much easier, and would allow for preloading much better. I havent tried the code, but here is what i would do (off my head)

    Code:
    <script language=javascript>
    
    function init() {
      var img1 = Image();
      var img2 = Image();
    
      img1.src = "imga.gif";
      img2.src = "imgb.gif";
    
      if(document.images[0].src == img1.src) {
         cng(img2.src);
      }
      else {
         cng(img1.src);
      }
    }
    
    function cng(imgSrc) {
      document.images[0].src = imgSrc;
    }
    
    </script>
    now you could write the randomise image function so that it changes every 1500 milliseconds or whatever, but i think that may work for both browsers, and that is how i would do it.
    Mag-Net's Home
    Visual Studio 6-Enterprise - SP4
    ICQ: 35519773
    Have Fun

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 1999
    Location
    London, England
    Posts
    213
    Thanks Mag-Net.

    However, I am having problems putting it all together. Could you show me how to code the randomised timer function to get this thing working for me.

    thanks,
    nmretd.

  4. #4
    Lively Member
    Join Date
    Mar 2000
    Location
    Lowestoft
    Posts
    91
    well mate, you had it in you code I think, lemme c what i can whip up from my head again..I bet when I have finished it still wont work, but i am too lazy to actually test it out myself, ok here goes:

    Code:
    <html>
    <head>
      <title>JS to Rotate Banners</title>
    </head>
    <script language=javascript type="text/javascript">
      var img1 = Image();  //global
      var img2 = Image();  //variables
    
    function init() {
      img1.src = "img1.jpg"; //this will preload
      img2.src = "img2.jpg"; //the images
    
      setInterval("fPrepare()", 1500); //set the time u want
    }
    
    function fPrepare() {
      if(document.images['banner'].src == img1.src) {
         cng(img2.src); //img1 & 2.src should be visible
                        //to this function as it is global
      }
      else {
         cng(img1.src);
      }
    }
    
    function cng(imgSrc) {
      document.images['banner'].src = imgSrc;
    }
    
    </script>
    
    <body onLoad="init()">
    <img src="" id="banner" width="200" height="20">
    </body>
    </html>
    This i am afraid isn't the best code I have ever written but I hope you can learn from it, and implement it to work better (if it works)...it is very sloppy, but the best i can do without testing it, and with no references, but I think that should all work with both browsers, after all it is very simple code. If you need more help then I will have to install netscape and try it myself.
    Mag-Net's Home
    Visual Studio 6-Enterprise - SP4
    ICQ: 35519773
    Have Fun

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 1999
    Location
    London, England
    Posts
    213
    Hi Mag-Net,

    I have made a slight amendment and have got it working on Explorer but it doesn't work in Netscape. Any chance you can look at it for me ?

    thanks,
    nmretd.

    <html>
    <head>
    <title>JS to Rotate Banners</title>
    </head>

    <script language=javascript>

    var img1 = new Image();
    var img2 = new Image();

    function init() {

    img1.src = "ban1.jpg";
    img2.src = "ban2.jpg";

    setInterval("fPrepare()", 1500);
    }

    function fPrepare() {
    if(document.images['banner'].src == img1.src) {
    cng(img2.src);

    }
    else {
    cng(img1.src);
    }
    }

    function cng(imgSrc) {
    document.images['banner'].src = imgSrc;
    }

    </script>

    <body onLoad="init()">
    <img src="" id="banner" width="400" height="60">

    </body>
    </html>

  6. #6
    Lively Member
    Join Date
    Mar 2000
    Location
    Lowestoft
    Posts
    91
    ok what is the error that you get in netscape? and also what was the ammendment u made? I cant see nething? also if you post code it makes like loads easier if you post it with the code tags around it, u have a sqaure bracket to open it and just like html where you would have: <code></code> u just exchange the angles with the square brackets.
    Mag-Net's Home
    Visual Studio 6-Enterprise - SP4
    ICQ: 35519773
    Have Fun

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 1999
    Location
    London, England
    Posts
    213
    You wrote:

    var img1 = Image(); //global
    var img2 = Image(); //variables

    I couldn't get it to work in IE so I changed this to the following:

    var img1 = new Image();
    var img2 = new Image();

    Also, Netscape doesn't give an error. The banners just don't appear on the page !

    Any ideas ?


  8. #8
    Lively Member
    Join Date
    Mar 2000
    Location
    Lowestoft
    Posts
    91
    Oh right ok I left out the new keyword...so shoot me. Well done for spotting it...

    So what you need to do is to put some alerts in the code to see what is happening and what is initialising. That is the easiest way to debug, and you will find your errors.
    Mag-Net's Home
    Visual Studio 6-Enterprise - SP4
    ICQ: 35519773
    Have Fun

  9. #9
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827

    Lightbulb

    You can put your code in code tags, and it will be displayed with formatting (ie indentation) preserved. You put the code in <CODE>...</CODE> tags, but instead of using the angled brackets <> You use the square brackets [].
    Harry.

    "From one thing, know ten thousand things."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width