Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    document.images won't work in a function?

    Hello codingforums! I'm new to javascript and have a problem regarding functions.

    I've been trying to create a slideshow, and while testing different syntax etc. I'm getting stuck at why this works:

    Code:
    document.images.portrait.src = img2.src;
    And not this:
    Code:
    function slideshow(){
    document.images.portrait.src = img2.src;
    }
    Thanks for any help!
    Last edited by CFre; 07-11-2011 at 02:40 PM.

  • #2
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    unless img2.src has been defined as a global variable then you have a scope issue because the function can't see any variables defined outside of it unless they are global variables.
    Last edited by bullant; 07-11-2011 at 02:14 PM.

  • #3
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    The image is defined in the <head> as following:

    Code:
    <script type="text/javascript">
    <!--
    var img1 = new Image();
    img1.src = "Photoshop/Portrait_Rebecca.png";
    var img2 = new Image();
    img2.src = "Photoshop/Portrait_Hans.png";
    //-->
    </script>
    Isn't the variables automaticly "global"(accesible wherever?) if defined this way?
    Last edited by CFre; 07-11-2011 at 02:40 PM.

  • #4
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I've seen several code examples using this within functions, and I'm having a hard time seeing what I'm doing wrong. Can gladly post the whole code if necessary, but as said above the image variables are declared in the <head> and I'm having no problems with switching images with code like this: document.images.portrait.src = img2.src;
    As long as its not within a function.

  • #5
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    yep, it is...but you're posting only bits and pieces of your code so obviously there is a problem elsewhere in your code that you haven't posted.

    My crystal ball isn't back from its 2500 year service yet so I can't use it to see the rest of your code.

  • #6
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    This works:
    Code:
    <head>
    
    <script type="text/javascript">
    <!--
    var img1 = new Image();
    img1.src = "Photoshop/Portrait_Rebecca.png";
    var img2 = new Image();
    img2.src = "Photoshop/Portrait_Hans.png";
    //-->
    </script>
    
    </head>
    <body>
    
    <img src="Photoshop/Portrait_Rebecca.png" name="portrait" width="340" height="260"/>
    <script type="text/javascript">
    document.images.portrait.src = img2.src;
    function slideshow(){
    /*document.images.portrait.src = img2.src;*/
    }
    
    </script>
    This doesn't:
    Code:
    <script type="text/javascript">
    <!--
    var img1 = new Image();
    img1.src = "Photoshop/Portrait_Rebecca.png";
    var img2 = new Image();
    img2.src = "Photoshop/Portrait_Hans.png";
    //-->
    </script>
    
    </head>
    <body>
    <img src="Photoshop/Portrait_Rebecca.png" name="portrait" width="340" height="260"/>
    <script type="text/javascript">
    /*document.images.portrait.src = img2.src;*/
    function slideshow(){
    document.images.portrait.src = img2.src;
    }
    
    </script>

  • #7
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    so where are you actually calling the function slideshow()?

    When you declare a function all it is doing is sitting there gathering dust until you actually call it.

  • Users who have thanked bullant for this post:

    CFre (07-11-2011)

  • #8
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Edit: Found the solution! body onload seems to work, thanks for the help
    Last edited by CFre; 07-11-2011 at 03:09 PM.


  •  

    Posting Permissions

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