• HEXUS
  • HEXUS.tv
  • channel
  • gaming
  • lifestyle
  • trust
  • community
  • ESReality
  • HEXUS.community discussion forumsVisit Corsair.com

    Welcome to the HEXUS.community discussion forums forums.

    You are currently viewing our boards as a guest which gives you limited access to view most discussions and other features. By joining our free community you will have access to post topics, respond to polls and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

    Go Back   HEXUS.community discussion forums > HEXUS.help - buying advice & technical queries > Operating systems & applications > Software and web development

    Software and web development Databases, graphics, programming, scripting and web development.

    Reply
     
    LinkBack Thread Tools
    Old 02-11-2009, 12:44 PM   #1 (permalink)
    VTECmeous
     
    Vimeous's Avatar
     
    Join Date: Aug 2003
    Posts: 1,861
    Thanks: 41
    Thanked 47 Times in 46 Posts
    Question Javascript: Timed Banner Not Working

    After yet more trawling for decent code and a healthly dislike for easy-way-out prebuilt code packs I've run into a javascript issue I've so far failed to leaver myself out of.

    Can you see where I've gone wrong?

    Aim: A timed banner which consists of multiple hidden divs'.
    Code:
    var c=0;
    var t;

    function timedBanner(divs)
    {
    switchBanner(c);
    c=c+1;
    if (c > divs -1) {
    c = 0;
    }
    t=setTimeout("timedBanner()",1000);
    }

    function switchBanner(obj) {

    var banners = new Array();
    banners[0] = document.getElementById("serviceBanner1").innerHTML;
    banners[1] = document.getElementById("serviceBanner2").innerHTML;

    document.getElementById("mainBanner").innerHTML = banners[obj];
    }
    timedBanner is called from the body onload and currently set to timedBanner(2)
    mainBanner is a div containing 2 hidden divs called serviceBanner1 and serviceBanner2

    Problem: The initial banner loads correctly. On the second pass I get an objectexpected error on the line starting banners[1]


    Why?

    No doubt it's a small problem but I can't see it at present. Help please!

    1 : Q9450 | 4Gb PC8500 DMX | ASUS P5E-WS Pro | HD2900XT | 2 x 750Gb (RAID1) | Adaptec ASR-2230SLP SCSI | DVDRW | Corsair 650W TX | CM RC-590 | 2x VP171s-2 | HP Ultrium 960 LTO3 | XP SP3 Pro
    2 : X2 4200+ | 2Gb PC3200 | ASUS A8N-SLI Premium | 6600GT | SonicFury | 8x 250Gb (2x RAID10) | 3Ware 9650SE-8LPML | DVDRW | Seasonic 700W | CM Stacker 830 | 2x VP171s-2 | XP SP3 Pro
    3 : Sm 3100+ | 1Gb PC3200 | ASUS K8N-E Deluxe | 9800 Pro | 4 x 250Gb Maxline III (RAID10) | DVDRW | CDRW | Tagan 530W Easycon | CM ATC-201B-BXT Black Widow | W7 RC
    W : Dell Precision 690 | Intel X5355 | 4Gb FBDIMM | Quadro FX3450 | 2x 146Gb 15k SAS (RAID1) | DVDRW | DVD | 2x 2007FP | XP SP3 Pro

    BBC News 29/09/08 : "It's the ferrets and weasels on the stock exchange that are to blame," said one pensioner.

    Last edited by Vimeous; 02-11-2009 at 01:21 PM..
    Vimeous is offline   Reply With Quote
    Old 02-11-2009, 08:44 PM   #2 (permalink)
    Three As fanboy...
     
    Join Date: Jan 2009
    Location: Manchester
    Posts: 1,600
    Thanks: 60
    Thanked 119 Times in 117 Posts
    scaryjim's system
    Re: Javascript: Timed Banner Not Working

    Code:
    function timedBanner(divs)
    {
        switchBanner(c);
        c=c+1;
        if (c > divs -1) {
            c = 0;
        }
    
        t=setTimeout("timedBanner()",1000);
    }
    
    Your issue is the line in bold: function timedBanner() expects a parameter! This should be:
    Code:
    t=setTimeout("timedBanner(divs)",1000);
    

    //
    // TODO: Add witty signature here
    //
    scaryjim is offline   Reply With Quote
    Received thanks from:
    Vimeous (03-11-2009)
    Old 03-11-2009, 12:13 AM   #3 (permalink)
    VTECmeous
     
    Vimeous's Avatar
     
    Join Date: Aug 2003
    Posts: 1,861
    Thanks: 41
    Thanked 47 Times in 46 Posts
    Re: Javascript: Timed Banner Not Working

    Originally Posted by scaryjim View Post
    Code:
    function timedBanner(divs)
    {
        switchBanner(c);
        c=c+1;
        if (c > divs -1) {
            c = 0;
        }
    
        t=setTimeout("timedBanner()",1000);
    }
    
    Your issue is the line in bold: function timedBanner() expects a parameter! This should be:
    Code:
    t=setTimeout("timedBanner(divs)",1000);
    
    Thanks Jim. I'd taken it out thinking it'd stop the loop running ad-infinitum but I see that would not be the case (and I was getting a different error).

    I now get an error that with "divs" being undefined.

    How is it best to define "divs" in this circumstance?

    1 : Q9450 | 4Gb PC8500 DMX | ASUS P5E-WS Pro | HD2900XT | 2 x 750Gb (RAID1) | Adaptec ASR-2230SLP SCSI | DVDRW | Corsair 650W TX | CM RC-590 | 2x VP171s-2 | HP Ultrium 960 LTO3 | XP SP3 Pro
    2 : X2 4200+ | 2Gb PC3200 | ASUS A8N-SLI Premium | 6600GT | SonicFury | 8x 250Gb (2x RAID10) | 3Ware 9650SE-8LPML | DVDRW | Seasonic 700W | CM Stacker 830 | 2x VP171s-2 | XP SP3 Pro
    3 : Sm 3100+ | 1Gb PC3200 | ASUS K8N-E Deluxe | 9800 Pro | 4 x 250Gb Maxline III (RAID10) | DVDRW | CDRW | Tagan 530W Easycon | CM ATC-201B-BXT Black Widow | W7 RC
    W : Dell Precision 690 | Intel X5355 | 4Gb FBDIMM | Quadro FX3450 | 2x 146Gb 15k SAS (RAID1) | DVDRW | DVD | 2x 2007FP | XP SP3 Pro

    BBC News 29/09/08 : "It's the ferrets and weasels on the stock exchange that are to blame," said one pensioner.
    Vimeous is offline   Reply With Quote
    Old 03-11-2009, 12:47 PM   #4 (permalink)
    Three As fanboy...
     
    Join Date: Jan 2009
    Location: Manchester
    Posts: 1,600
    Thanks: 60
    Thanked 119 Times in 117 Posts
    scaryjim's system
    Re: Javascript: Timed Banner Not Working

    Oops! sorry, I forgot that setTimeout takes a string for the command to run. You'll need to do
    Code:
    t=setTimeout("timedBanner(" + divs + ")",1000);
    
    to put the value of divs into the string. Apparently being a professional web app developer doesn't make you immune from stupid mistakes (who'd'a thunk it, huh? )!

    EDIT: it doesn't make me immune to writing questionable / deprecated code, either! There's two versions of setTimeout, the one you're using which runs some text input as a code snippet, and a newer version that takes a function object (I won't go into the complexity of *everything* being an object in javascript) that you can then add parameters to. The generalised form is: window.setTimeout(func, delay, [param1, param2, ...]);, so your code snippet should be
    Code:
    t=setTimeout(timedBanner, 1000, divs);
    

    //
    // TODO: Add witty signature here
    //

    Last edited by scaryjim; 03-11-2009 at 12:53 PM..
    scaryjim is offline   Reply With Quote
    Received thanks from:
    Vimeous (03-11-2009)
    Old 03-11-2009, 03:59 PM   #5 (permalink)
    VTECmeous
     
    Vimeous's Avatar
     
    Join Date: Aug 2003
    Posts: 1,861
    Thanks: 41
    Thanked 47 Times in 46 Posts
    Re: Javascript: Timed Banner Not Working

    Originally Posted by scaryjim View Post
    Oops! sorry, I forgot that setTimeout takes a string for the command to run. You'll need to do
    Code:
    t=setTimeout("timedBanner(" + divs + ")",1000);
    
    to put the value of divs into the string. Apparently being a professional web app developer doesn't make you immune from stupid mistakes (who'd'a thunk it, huh? )!

    EDIT: it doesn't make me immune to writing questionable / deprecated code, either! There's two versions of setTimeout, the one you're using which runs some text input as a code snippet, and a newer version that takes a function object (I won't go into the complexity of *everything* being an object in javascript) that you can then add parameters to. The generalised form is: window.setTimeout(func, delay, [param1, param2, ...]);, so your code snippet should be
    Code:
    t=setTimeout(timedBanner, 1000, divs);
    
    A definite improvement! ...... but now I'm back to the previous error:

    In IE7/8 I get an objectexpected error on the line starting banners[1].
    In Firefox 3.5 I get document.getElementById("serviceBanner1") is null.

    Both errors imply I've got the function switchBanner wrong. The HTML code for divs themselves is:
    <div id="mainBanner">

    <div id="serviceBanner1" class="hide">
    <img src="image1.jpg" class="" alt="x" title="x" width="835" height="350" border="0" usemap="#xMap1" />
    <map name="xMap1" id="xMap1">
    <area shape="rect" coords="460,70,615,120" href="x.html" title="x" alt="x" class="cursor" />
    </map>
    </div>

    <div id="serviceBanner2" class="hide">
    <img src="image2.jpg" class="" alt="y" title="y" width="835" height="350" border="0" usemap="#yMap1" />
    <map name="yMap1" id="yMap1">
    <area shape="rect" coords="485,175,645,220" href="y.html" title="y" alt="y" class="cursor" />
    </map>
    </div>

    </div>
    I wonder if it's related to the divs being hidden.
    That said both browsers display the first banner and only trigger an error when the running the subsequent code.

    Confused!


    n.b.
    Sorry if you cringed at the lack of class for the images - that'll appear if it starts working

    1 : Q9450 | 4Gb PC8500 DMX | ASUS P5E-WS Pro | HD2900XT | 2 x 750Gb (RAID1) | Adaptec ASR-2230SLP SCSI | DVDRW | Corsair 650W TX | CM RC-590 | 2x VP171s-2 | HP Ultrium 960 LTO3 | XP SP3 Pro
    2 : X2 4200+ | 2Gb PC3200 | ASUS A8N-SLI Premium | 6600GT | SonicFury | 8x 250Gb (2x RAID10) | 3Ware 9650SE-8LPML | DVDRW | Seasonic 700W | CM Stacker 830 | 2x VP171s-2 | XP SP3 Pro
    3 : Sm 3100+ | 1Gb PC3200 | ASUS K8N-E Deluxe | 9800 Pro | 4 x 250Gb Maxline III (RAID10) | DVDRW | CDRW | Tagan 530W Easycon | CM ATC-201B-BXT Black Widow | W7 RC
    W : Dell Precision 690 | Intel X5355 | 4Gb FBDIMM | Quadro FX3450 | 2x 146Gb 15k SAS (RAID1) | DVDRW | DVD | 2x 2007FP | XP SP3 Pro

    BBC News 29/09/08 : "It's the ferrets and weasels on the stock exchange that are to blame," said one pensioner.

    Last edited by Vimeous; 03-11-2009 at 04:11 PM..
    Vimeous is offline   Reply With Quote
    Old 03-11-2009, 04:49 PM   #6 (permalink)
    Three As fanboy...
     
    Join Date: Jan 2009
    Location: Manchester
    Posts: 1,600
    Thanks: 60
    Thanked 119 Times in 117 Posts
    scaryjim's system
    Re: Javascript: Timed Banner Not Working

    Originally Posted by Vimeous View Post
    A definite improvement! ...... but now I'm back to the previous error:

    In IE7/8 I get an objectexpected error on the line starting banners[1].
    In Firefox 3.5 I get document.getElementById("serviceBanner1") is null.

    Both errors imply I've got the function switchBanner wrong.

    <snip>
    Hmm, you have indeed got the code for switchBanner wrong! The first time the code is called, it rewrites over the whole of the content of mainBanner with the content of serviceBanner1. This [b]writes over[/i] the content of both serviceBanner1 and servicebanner2, so they no longer exist in the document tree. Any future attempt to reference either of them is doomed to failure, because they don't exist anymore!

    Sorry, I didn't even look at your switchBanner code before because I'd spotted a problem in your earlier code (and frankly I wasn't treating this like a formal code review ).

    if you want to do it this way I *think* setting up the banners array outside of the function might work.

    A better way to change whether you can see a DIV programatically is to set its .style.display property:
    Code:
    document.getElementById("serviceBanner1").style.display = "block";
    document.getElementById("serviceBanner2").style.display = "none";
    
    will turn off display of serviceBanner2 and show serviceBanner1 instead. While you've only got 2 or 3 divs to roll through this works OK: more than that and you need to look at enumerating the children of a parent (in this case your "mainBanner" div). But for now I'd play with using your c variable with an if statement and doing it manually, just to get it working: there's a few complexities and pitfalls to watch out for when enumerating the children of an element...

    Originally Posted by Vimeous View Post
    Sorry if you cringed at the lack of class for the images - that'll appear if it starts working
    I'm not concerned by the lack of a class so much as putting the attribute in when you're not using it but as you say, that'll come with time...

    //
    // TODO: Add witty signature here
    //
    scaryjim is offline   Reply With Quote
    Received thanks from:
    Vimeous (05-11-2009)
    Old 05-11-2009, 04:08 PM   #7 (permalink)
    VTECmeous
     
    Vimeous's Avatar
     
    Join Date: Aug 2003
    Posts: 1,861
    Thanks: 41
    Thanked 47 Times in 46 Posts
    Re: Javascript: Timed Banner Not Working

    Originally Posted by scaryjim View Post
    Hmm, you have indeed got the code for switchBanner wrong! The first time the code is called, it rewrites over the whole of the content of mainBanner with the content of serviceBanner1. This [b]writes over[/i] the content of both serviceBanner1 and servicebanner2, so they no longer exist in the document tree. Any future attempt to reference either of them is doomed to failure, because they don't exist anymore!

    Sorry, I didn't even look at your switchBanner code before because I'd spotted a problem in your earlier code (and frankly I wasn't treating this like a formal code review ).

    if you want to do it this way I *think* setting up the banners array outside of the function might work.

    A better way to change whether you can see a DIV programatically is to set its .style.display property:
    Code:
    document.getElementById("serviceBanner1").style.display = "block";
    document.getElementById("serviceBanner2").style.display = "none";
    
    will turn off display of serviceBanner2 and show serviceBanner1 instead. While you've only got 2 or 3 divs to roll through this works OK: more than that and you need to look at enumerating the children of a parent (in this case your "mainBanner" div). But for now I'd play with using your c variable with an if statement and doing it manually, just to get it working: there's a few complexities and pitfalls to watch out for when enumerating the children of an element...

    I'm not concerned by the lack of a class so much as putting the attribute in when you're not using it but as you say, that'll come with time...

    BIG thanks for pointing out I was overwriting the code. I'm definitely not a pro-web-app-dev!

    That said I reckon I can get an alternate solution working to those you suggest. Instead of replacing the content of mainBanner I just need to add an empty named div as the content of mainBanner is hidden. Then I can replace the content of that as often as I like

    Of course I've just tried this and I seem to have too many entires ...... but it is has shown divs 1 and 2 this time lol
    (actually it works in firefox but ie is declaring issues with undefined stuff hmm.....)

    1 : Q9450 | 4Gb PC8500 DMX | ASUS P5E-WS Pro | HD2900XT | 2 x 750Gb (RAID1) | Adaptec ASR-2230SLP SCSI | DVDRW | Corsair 650W TX | CM RC-590 | 2x VP171s-2 | HP Ultrium 960 LTO3 | XP SP3 Pro
    2 : X2 4200+ | 2Gb PC3200 | ASUS A8N-SLI Premium | 6600GT | SonicFury | 8x 250Gb (2x RAID10) | 3Ware 9650SE-8LPML | DVDRW | Seasonic 700W | CM Stacker 830 | 2x VP171s-2 | XP SP3 Pro
    3 : Sm 3100+ | 1Gb PC3200 | ASUS K8N-E Deluxe | 9800 Pro | 4 x 250Gb Maxline III (RAID10) | DVDRW | CDRW | Tagan 530W Easycon | CM ATC-201B-BXT Black Widow | W7 RC
    W : Dell Precision 690 | Intel X5355 | 4Gb FBDIMM | Quadro FX3450 | 2x 146Gb 15k SAS (RAID1) | DVDRW | DVD | 2x 2007FP | XP SP3 Pro

    BBC News 29/09/08 : "It's the ferrets and weasels on the stock exchange that are to blame," said one pensioner.

    Last edited by Vimeous; 05-11-2009 at 04:18 PM..
    Vimeous is offline   Reply With Quote
    Old 06-11-2009, 08:35 AM   #8 (permalink)
    VTECmeous
     
    Vimeous's Avatar
     
    Join Date: Aug 2003
    Posts: 1,861
    Thanks: 41
    Thanked 47 Times in 46 Posts
    Talking Fixed ..... interesting .....

    Originally Posted by scaryjim View Post
    Code:
    t=setTimeout("timedBanner(" + divs + ")",1000);
    
    Code:
    t=setTimeout(timedBanner, 1000, divs);
    
    scary I've double checked the code again and it turns out only the longer original code you suggested (top one) will work with both Firefox 3.5 and IE 7. I'll check IE 8 shortly.

    Many thanks for your help.
    While I know I've 'cheated' by using another empty div it also keeps the code simpler for myself and those who follow to maintain.

    Now I'm off to canabalise the fade in/out I discovered recently

    1 : Q9450 | 4Gb PC8500 DMX | ASUS P5E-WS Pro | HD2900XT | 2 x 750Gb (RAID1) | Adaptec ASR-2230SLP SCSI | DVDRW | Corsair 650W TX | CM RC-590 | 2x VP171s-2 | HP Ultrium 960 LTO3 | XP SP3 Pro
    2 : X2 4200+ | 2Gb PC3200 | ASUS A8N-SLI Premium | 6600GT | SonicFury | 8x 250Gb (2x RAID10) | 3Ware 9650SE-8LPML | DVDRW | Seasonic 700W | CM Stacker 830 | 2x VP171s-2 | XP SP3 Pro
    3 : Sm 3100+ | 1Gb PC3200 | ASUS K8N-E Deluxe | 9800 Pro | 4 x 250Gb Maxline III (RAID10) | DVDRW | CDRW | Tagan 530W Easycon | CM ATC-201B-BXT Black Widow | W7 RC
    W : Dell Precision 690 | Intel X5355 | 4Gb FBDIMM | Quadro FX3450 | 2x 146Gb 15k SAS (RAID1) | DVDRW | DVD | 2x 2007FP | XP SP3 Pro

    BBC News 29/09/08 : "It's the ferrets and weasels on the stock exchange that are to blame," said one pensioner.
    Vimeous is offline   Reply With Quote
    Old 06-11-2009, 01:17 PM   #9 (permalink)
    Three As fanboy...
     
    Join Date: Jan 2009
    Location: Manchester
    Posts: 1,600
    Thanks: 60
    Thanked 119 Times in 117 Posts
    scaryjim's system
    Re: Fixed ..... interesting .....

    Originally Posted by Vimeous View Post
    scary I've double checked the code again and it turns out only the longer original code you suggested (top one) will work with both Firefox 3.5 and IE 7.
    Hmm, quite possible. IE prior to 8 didn't bother about following the standards too closely! if there's a version that works with everything stick with that one

    You should find the second version works OK in IE8 I think - in my experience so far they've done a much better job of making it adhere to the standard... my biggest problem is that some of the sites I design for a living are targetted at the NHS and some NHS trusts still haven't upgraded from IE6 because their in-house developed web apps are coded for specific IE6 hacks, so not only do I have to code for several different browsers, I also have to be prepared for 3 different versions of IE!

    //
    // TODO: Add witty signature here
    //
    scaryjim is offline   Reply With Quote
    Old 07-11-2009, 07:10 PM   #10 (permalink)
    Large Member
     
    Join Date: Apr 2004
    Posts: 3,368
    Thanks: 25
    Thanked 27 Times in 22 Posts
    Re: Javascript: Timed Banner Not Working

    Or you could just use jquery and not spend 4 days completeing the simplest of scripts

    To err is human. To really foul things up ... you need a computer.
    yamangman is offline   Reply With Quote
    Reply

    Breadcrumb
    Go Back   HEXUS.community discussion forums > HEXUS.help - buying advice & technical queries > Operating systems & applications > Software and web development


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Trackbacks are On
    Pingbacks are On
    Refbacks are On


    Similar Threads
    Thread Thread Starter Forum Replies Last Post
    Adsense Banner - How much money? oshta General discussion 12 23-10-2009 11:12 AM
    Very odd issue: Some website's images won't load TheVoice Help - technical & advisory 10 29-03-2007 04:18 PM
    Pipex problems, DOS attacks or ? ? Carnagerover Networking and Broadband 14 25-01-2007 01:14 PM
    CS:S Lag problems NoExcuse Help - technical & advisory 28 06-02-2006 04:31 PM
    Simple Banner Script Javascript or PHP Advice ikonia Software and web development 2 12-07-2005 07:29 PM



    All times are GMT. The time now is 09:59 PM.

    Any representations/statements made on the HEXUS.community discussion forums are the representations/statements of the author i.e. the person/organisation making them. If any such representations/statements are disputed they are a matter between the parties concerned.
    HEXUS Limited accepts no responsibility for any misrepresentations, inaccurate or false statements made by any person/organisation other than HEXUS Limited employees.
    For more information please read HEXUS Limited's terms, conditions and privacy policy.

    Hosted Exchange

    Powered by vBulletin® Version 3.8.4
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Content Relevant URLs by vBSEO 3.3.2
    © Copyright 2009 HEXUS® Limited. All rights reserved. Unauthorised reproduction strictly prohibited.