/    Sign up×
Community /Pin to ProfileBookmark

global variables in javascript

Hi… can i have something like:


————————————————————————

  • 1. javascript global variable declaration.

  • 2. javascript function 1

  • 3. javascript function 2
  • when i click a link function 1 is called to store values in the global variables and a popup window opens.

    when i close the popup window, some values are submitted to the parent page.

    Also the parent page refreshes.

    And function 2 is called to display the values stored in the global variables


    ————————————————————————–

    Thank u!

    to post a comment
    JavaScript

    6 Comments(s)

    Copy linkTweet thisAlerts:
    @TheBearMayFeb 03.2006 — <i>
    </i>...
    &lt;script type="text/javascript"&gt;
    var global1;

    function fun1(){
    global1=window.prompt("Enter value for global1");
    document.getElementById("oDiv").innerHTML = global1;
    fun2();
    }

    function fun2(){
    alert(global1);
    }
    &lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;div id="oDiv"&gt;&lt;/div&gt;
    ...
    Copy linkTweet thisAlerts:
    @konithomimoFeb 03.2006 — Luckily JS treats variables different than most other languages, so you can redefine values globally instead of only being able to reassign then locally. Otherwise code like Bear's wouldn't work properly.
    Copy linkTweet thisAlerts:
    @php_dudetteauthorFeb 04.2006 — I dont want fun1 to call fun2.

    fun2 should be called using another form event.

    Thanx. :-)
    Copy linkTweet thisAlerts:
    @konithomimoFeb 04.2006 — That won't work in JS. The reason why it wont . . . if you call function1, which stores values into the global variables, then those values are only held while function1 is running, and only hold for function1 and functions that function1 calls. (the values only hold locally) That being said, if function1 has a different value locally for a global variable, and another function (which uses the global variable) is called, but not from, function1, then the global variables will have their original values for the new function.
    Copy linkTweet thisAlerts:
    @TheBearMayFeb 06.2006 — That won't work in JS. The reason why it wont . . . if you call function1, which stores values into the global variables, then those values are only held while function1 is running, and only hold for function1 and functions that function1 calls. (the values only hold locally) That being said, if function1 has a different value locally for a global variable, and another function (which uses the global variable) is called, but not from, function1, then the global variables will have their original values for the new function.[/QUOTE]
    Funny, my JS seems to update the global and hold the value outside of the function. Wouldn't be much use if they didn't.
    Copy linkTweet thisAlerts:
    @cbrookesFeb 06.2006 — That won't work in JS. The reason why it wont . . . if you call function1, which stores values into the global variables, then those values are only held while function1 is running, and only hold for function1 and functions that function1 calls. (the values only hold locally) That being said, if function1 has a different value locally for a global variable, and another function (which uses the global variable) is called, but not from, function1, then the global variables will have their original values for the new function.[/QUOTE]

    Actually the only way to guarentee that variables are NOT global is define them using 'var'. This ALSO applies to variables which are first instantiated in a function!

    :o
    ×

    Success!

    Help @php_dudette spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.2,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,
    )...