1

I have declared the following variable outside of an external js file because part of it is generated server side.

<script type="text/javascript">
     var images=new Array(<%= Master.slideshowArray %>);
</script>

For some reason, by removing this from the external js file (below) the slideshow no longer works... I am guessing I've made an error declaring it as a global variable or perhaps there's something else I need to declare globally... Any ideas?

var nextimage=0;

doSlideshow();

function doSlideshow()
{
    if($('.backgroundImage').length!=0)
    {
        $('.backgroundImage').fadeOut(500,function(){slideshowFadeIn();$(this).remove();});
    }
    else
    {
        slideshowFadeIn();
    }
}

function slideshowFadeIn()
{
    if(nextimage>=images.length) 
        nextimage=0;

    $('.homeLeadContent').prepend($('<img class="backgroundImage" src="'+images[nextimage++]+'" style="display:none;">').fadeIn(500,function() {
        setTimeout(doSlideshow,1000);
    }));
}
3
  • DOM already defines a global variable called images. Have your tried a different name? Commented Mar 8, 2011 at 9:50
  • tried changing images to myimages... still no joy. Commented Mar 8, 2011 at 10:11
  • What does the generated inline script tag look like? Are you sure there aren't syntax errors causing that script tag to fail to execute? Commented Mar 8, 2011 at 10:59

1 Answer 1

1

Does the script tag for the external js file come before the var images=... inline script tag?

Browsers execute code in the order that they see them, so if the external js file is seen first, it'll execute the doSlideShow() function which may call the slideshowFadeIn() which would try and references a non-existent-at-that-point images variable.

Sign up to request clarification or add additional context in comments.

5 Comments

Nope... the inline script tag with the var images comes first, then the external file...
If you do alert(typeof(images)); on the line after var images=..., do you get an alert showing object or something kosher, i.e. you don't want to see 'undefined'?
So if I see 'undefined' (which I did), what would be the next thing to do...?
Then there's something wrong with your var images=... line. You should be able to extract that particular line into a local HTML file and debug using FireBug or some other JavaScript debugger. If you showed that line to us, someone might point out the problem.
Firebug revealed that I had a typo in the link to the external file... all else is good now - declared as object! thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.