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 12 of 12
  1. #1
    Regular Coder
    Join Date
    Oct 2002
    Posts
    147
    Thanks
    31
    Thanked 2 Times in 2 Posts

    object wont alert in a function that I call from a function

    Hi

    Thanks for the help. I'm trying to get my head around this stuff.

    Anyway here is the question.

    Why won't the alert(arr[1].src); work in the showMe(); function

    I don't get an error saying what I'm doing wrong

    any ideas?

    Thanks

    Andrew


    Code:
    firstStep();
    
    function firstStep(){
    
    	var arr = new Array()
    	
    	for(var i=1;i<=6;i++){
    		
    			arr[i] = new Image();
    			arr[i].src = "images/no"+i+".jpg";
    					
    			alert(arr[i].src);
    		}
    		
    	showMe();	
    		
    }
    
    function showMe(){
    
    
    alert(arr[1].src);
    
    }
    Last edited by andrew1234; 07-18-2009 at 07:03 PM.

  • #2
    New Coder
    Join Date
    Jun 2009
    Posts
    18
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Because arrImages is not declared anywhere?!

  • #3
    Regular Coder
    Join Date
    Oct 2002
    Posts
    147
    Thanks
    31
    Thanked 2 Times in 2 Posts
    Hi

    Thanks for the reply

    Sorry about that I thought I fixed that in the first post.

    Still the alert(arr[1].src); work in the showMe(); function

    any ideas?

  • #4
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    Why won't the alert(arr[1].src); work in the showMe(); function
    You have declared the arr variable as a local variable and that is why you cannot refer it anywhere outside the function firstStep().
    I am still learning English

  • Users who have thanked Amphiluke for this post:

    andrew1234 (07-18-2009)

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Because as Amphiluke says you have defined the array arr as local to the function firstStep().

    Change to:-

    var arr = new Array(); // declare the array outside the function to make it global
    firstStep();
    function firstStep() {

    Quizmaster: Which Boris recently became Mayor of London?
    Contestant: Yeltsin.

  • Users who have thanked Philip M for this post:

    andrew1234 (07-18-2009)

  • #6
    Regular Coder
    Join Date
    Oct 2002
    Posts
    147
    Thanks
    31
    Thanked 2 Times in 2 Posts
    thanks ... now it works ...yeee haaaaaaa

    so am I understanding this correctly.


    if I declare the var inside the function it is only global in that function?

  • #7
    Regular Coder
    Join Date
    Oct 2002
    Posts
    147
    Thanks
    31
    Thanked 2 Times in 2 Posts
    thanks for the help

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by andrew1234 View Post
    if I declare the var inside the function it is only global in that function?
    Oh dear! No, if you declare variable with var within a function, the variable is local to that function. If you declare a variable (with or without var) outside all functions, then that variable is global, meaning accessible to all functions in the program. This is called the scope of the variable. OK?

    For more info see:- http://www.webdevelopersnotes.com/tu...avascript.php3
    Last edited by Philip M; 07-18-2009 at 07:23 PM.

  • Users who have thanked Philip M for this post:

    andrew1234 (07-18-2009)

  • #9
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    The variable arr that you have declared this way:
    Code:
    var arr = new Array()
    is a private variable inside the function firstStep() so the other method showMe will not be able to access it.

    it should be declared as global variable outside the scope of the function like this
    Code:
    var arr = new Array();
    firstStep();
    
    function firstStep(){
    
    	
    	
    	for(var i=1;i<=6;i++){
    		
    			arr[i] = new Image();
    			arr[i].src = "images/no"+i+".jpg";
    					
    			alert(arr[i].src);
    		}
    		
    	showMe();	
    		
    }
    
    function showMe(){
    
    
    alert(arr[1].src);
    
    }
    and by the way use semicolons at the end of each javascript command.

  • Users who have thanked ckeyrouz for this post:

    andrew1234 (07-18-2009)

  • #10
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    sorry for the reply I did not see the other reply.

  • #11
    Regular Coder
    Join Date
    Oct 2002
    Posts
    147
    Thanks
    31
    Thanked 2 Times in 2 Posts
    thanks for the help..everyone

    Heres one more question.

    Is it possible to make a variable global so that all other functions can use it.But it is declared inside the function test();

    eg

    Code:
    function test(){
    
    var calcAnswer;
    //Can it be done?
    
    
    }

  • #12
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    Never tried it before but you might try this:

    function test()
    {
    var tempVar = "tmp";
    this.calcAnswer = tempVar ;

    }

    I never tried such thing but I think it works.
    Did not try it.
    And if it works, nake sure that you assign to it the last value.


  •  

    Posting Permissions

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