JavaScript Image Rotation Issues

    Date: 12/16/05 (WebDesign)    Keywords: web, seo

    I've been using a bit of script that loads a random image, URL and text into a page each time the page is loaded.

    This seems to work fine for up to 15 images/arrays, but over that, it starts to complain that:
    "Error: 'image.href' is null or not an object"

    Here's the code:

    function create() {
            this.width = ''
            this.height = ''
            this.src = ''
            this.href = ''
            this.border = ''
            this.mouseover = ''
            this.sponsor = ''
    }
    scs = new Array()
    for(var i=1; i<=19; i++) { scs[i] = new create() }
    
    scs[1].width = "437"
    scs[1].height = "292"
    scs[1].src = "images/splash/Ritz.jpg"
    scs[1].href = "http://www.theritzlondon.com/"
    scs[1].border = "0"
    scs[1].mouseover = "Click here to visit The Ritz"
    scs[1].sponsor = "The Ritz"
    
    scs[2].width = "400"
    scs[2].height = "316"
    scs[2].src = "images/splash/nhac.jpg"
    scs[2].href = "http://www.nh-hotels.com/portal/page?_pageid=9,424&_dad=porweb&_schema=PORTAL30"
    scs[2].border = "0"
    scs[2].mouseover = "Click here to visit NH Hoteles"
    scs[2].sponsor = "NH Hoteles - The Amsterdam Centre Hotel"
    
    scs[3].width = "400"
    scs[3].height = "266"
    scs[3].src = "images/splash/nhcc.jpg"
    scs[3].href = "http://www.nh-hotels.com/portal/page?_pageid=9,424&_dad=porweb&_schema=PORTAL30"
    scs[3].border = "0"
    scs[3].mouseover = "Click here to visit NH Hoteles"
    scs[3].sponsor = "NH Hoteles - The City Centre Hotel"
    
    scs[4].width = "400"
    scs[4].height = "316"
    scs[4].src = "images/splash/nhdh.jpg"
    scs[4].href = "http://www.nh-hotels.com/portal/page?_pageid=9,424&_dad=porweb&_schema=PORTAL30"
    scs[4].border = "0"
    scs[4].mouseover = "Click here to visit NH Hoteles"
    scs[4].sponsor = "NH Hoteles - The Doelen Hotel"
    
    scs[5].width = "318"
    scs[5].height = "400"
    scs[5].src = "images/splash/nhghk.jpg"
    scs[5].href = "http://www.nh-hotels.com/portal/page?_pageid=9,424&_dad=porweb&_schema=PORTAL30"
    scs[5].border = "0"
    scs[5].mouseover = "Click here to visit NH Hoteles"
    scs[5].sponsor = "NH Hoteles - The Grand Hotel Krasnapolsky"
    
    scs[6].width = "400"
    scs[6].height = "319"
    scs[6].src = "images/splash/nhsh.jpg"
    scs[6].href = "http://www.nh-hotels.com/portal/page?_pageid=9,424&_dad=porweb&_schema=PORTAL30"
    scs[6].border = "0"
    scs[6].mouseover = "Click here to visit NH Hoteles"
    scs[6].sponsor = "NH Hoteles - The Schiller Hotel"
    
    scs[7].width = "400"
    scs[7].height = "320"
    scs[7].src = "images/splash/hch.jpg"
    scs[7].href = "http://www.hastingshotels.com"
    scs[7].border = "0"
    scs[7].mouseover = "Click here to visit Hastings Hotels"
    scs[7].sponsor = "The Hastings Culloden Hotel"
    
    scs[8].width = "400"
    scs[8].height = "369"
    scs[8].src = "images/splash/hmb.jpg"
    scs[8].href = "http://www.hotelmajestic.es"
    scs[8].border = "0"
    scs[8].mouseover = "Click here to visit the Hotel Majestic Barcelona"
    scs[8].sponsor = "The Hotel Majestic Barcelona"
    
    scs[9].width = "400"
    scs[9].height = "422"
    scs[9].src = "images/splash/hi.jpg"
    scs[9].href = "http://www.hotel-inglaterra.com"
    scs[9].border = "0"
    scs[9].mouseover = "Click here to visit the Hotel Inglaterra"
    scs[9].sponsor = "The Hotel Inglaterra"
    
    scs[10].width = "400"
    scs[10].height = "275"
    scs[10].src = "images/splash/hlb.jpg"
    scs[10].href = "http://www.lebristolparis.com"
    scs[10].border = "0"
    scs[10].mouseover = "Click here to visit the Hotel Le Bristol"
    scs[10].sponsor = "The Hotel Le Bristol, Paris"
    
    scs[11].width = "400"
    scs[11].height = "403"
    scs[11].src = "images/splash/hmraa.jpg"
    scs[11].href = "http://www.solmelia.com"
    scs[11].border = "0"
    scs[11].mouseover = "Click here to visit the Hotel Melia Roma Aurelia Antica"
    scs[11].sponsor = "The Hotel Melia Roma Aurelia Antica"
    
    scs[12].width = "300"
    scs[12].height = "414"
    scs[12].src = "images/splash/h71.jpg"
    scs[12].href = "http://www.hotel71.com"
    scs[12].border = "0"
    scs[12].mouseover = "Click here to visit Hotel 71"
    scs[12].sponsor = "Hotel 71, Chicago"
    
    scs[13].width = "267"
    scs[13].height = "339"
    scs[13].src = "images/splash/ebhih.jpg"
    scs[13].href = "http://www.hiexpresshammersmith.co.uk/"
    scs[13].border = "0"
    scs[13].mouseover = "Click here to visit Somerston Hotels - Express by Holiday Inn Hammersmith"
    scs[13].sponsor = "Somerston Hotels - Express by Holiday Inn Hammersmith"
    
    scs[14].width = "435"
    scs[14].height = "336"
    scs[14].src = "images/splash/rg.jpg"
    scs[14].href = "http://www.royalgardenhotel.co.uk/"
    scs[14].border = "0"
    scs[14].mouseover = "Click here to visit the Royal Garden Hotel"
    scs[14].sponsor = "The Royal Garden Hotel"
    
    scs[15].width = "440"
    scs[15].height = "320"
    scs[15].src = "images/splash/cml.jpg"
    scs[15].href = "http://www.lucienbarriere.com/localized/en/portail/hotels/nos_etablissements/castel_marie_louise/index.asp?choix_1=la_baule&choix_2=castel_marie_louise"
    scs[15].border = "0"
    scs[15].mouseover = "Click here to visit the Lucien Barriere, Castel Marie-Louise"
    scs[15].sponsor = "Lucien Barriere, Castel Marie-Louise"
    
    scs[16].width = "435"
    scs[16].height = "336"
    scs[16].src = "images/splash/hb.jpg"
    scs[16].href = "http://www.lucienbarriere.com/localized/en/portail/hotels/nos_etablissements/hermitage/index.asp?choix_1=la_baule&choix_2=hermitage"
    scs[16].border = "0"
    scs[16].mouseover = "Click here to visit the Lucien Barriere, Hermitage Barriere"
    scs[16].sponsor = "Lucien Barriere, Hermitage Barriere"
    
    scs[17].width = "435"
    scs[17].height = "336"
    scs[17].src = "images/splash/hb.jpg"
    scs[17].href = "http://www.lucienbarriere.com/localized/en/portail/hotels/nos_etablissements/hermitage/index.asp?choix_1=la_baule&choix_2=hermitage"
    scs[17].border = "0"
    scs[17].mouseover = "Click here to visit the Lucien Barriere, Hermitage Barriere"
    scs[17].sponsor = "Lucien Barriere, Hermitage Barriere"
    
    scs[18].width = "435"
    scs[18].height = "336"
    scs[18].src = "images/splash/hb.jpg"
    scs[18].href = "http://www.lucienbarriere.com/localized/en/portail/hotels/nos_etablissements/hermitage/index.asp?choix_1=la_baule&choix_2=hermitage"
    scs[18].border = "0"
    scs[18].mouseover = "Click here to visit the Lucien Barriere, Hermitage Barriere"
    scs[18].sponsor = "Lucien Barriere, Hermitage Barriere"
    
    scs[19].width = "435"
    scs[19].height = "336"
    scs[19].src = "images/splash/hb.jpg"
    scs[19].href = "http://www.lucienbarriere.com/localized/en/portail/hotels/nos_etablissements/hermitage/index.asp?choix_1=la_baule&choix_2=hermitage"
    scs[19].border = "0"
    scs[19].mouseover = "Click here to visit the Lucien Barriere, Hermitage Barriere"
    scs[19].sponsor = "Lucien Barriere, Hermitage Barriere"
    
    var n = Math.random() + ''
    n = parseInt(n.charAt(19))
    if(n >19) {
            n = n - 19
    }
    else if(n==0) {
            n = n + 19
    }
    n += ""
    
    var image = scs[n]
    var sc = ""
    sc += ' \n'
    sc += '


    ' + image.sponsor + '
    '


    The script is loaded from the head as an external .js file and called in the body as this:


    Any ideas why it starts failing after 15 arrays? I've tried 'easier' URLs and different images to no avail.

    I've also tried a different script within the body since, but this one rotates the images all the time and too quickly. Any ideas on how to slow it down so that the images are changed say every 20 seconds (20000 milliseconds)? I've tried some setTimeouts in various places but can't seem to slow it down. Code:

    Source: http://www.livejournal.com/community/webdesign/1031171.html

« How to Manage the Monster -... || Simple, simple, simple... »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home