dcsimg
www.webdeveloper.com
Results 1 to 2 of 2

Thread: accessing local variables from other functions

Hybrid View

  1. #1
    Join Date
    May 2012
    Posts
    1

    accessing local variables from other functions

    I want to do the following in my script:

    Declare 3 variables in one function and call this function on window load. After that use values of those 3 variables in my other functions. Any option to do this without having to declare those variables outside the function as globals?

    Again, it is important that the variables are declared inside a function which will be called on page load.

    HTML code:

    <a href=''onclick='_default();return false;'>Link</a>
    <div id='mydiv'></div>

    Javascript:

    function _default(){

    var x = document.getElementById('mydiv');
    var h = x.offsetHeight;

    _main(x,h);

    }

    _default();

    function _main(x,h) {

    alert(x);
    alert(h);

    }

    You will see that this script executes _default() on page load. _default() function passes 2 parameters to _main() and executes _main() function throwing 2 alerts with variables x and h. But how do I make it so that those 2 alerts are thrown when I click on the link (see html code above)?

  2. #2
    Join Date
    May 2012
    Posts
    14
    You could declare a global array, and store these 2 variables into this array.

    You could also think of Application cache if you are in HTML5 world

    Having said that, I guess the only option is to declare it outside and use it inside. Something like the following

    <a href=''onclick='_main();return false;'>Link</a>
    <div id='mydiv'></div>

    javascript:
    [COLOR="rgb(46, 139, 87)"]var x ="";
    var h ="";[/COLOR]
    function _default(){

    [COLOR="rgb(46, 139, 87)"]x = document.getElementById('mydiv');
    h = x.offsetHeight;[/COLOR]

    _main(x,h);

    }

    _default();

    function _main(x,h) {

    alert(x);
    alert(h);

    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles