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

Thread: Question about the scope and availability of vars

  1. #1
    Join Date
    Apr 2005
    Posts
    493

    Question about the scope and availability of vars

    Question: if I declare a variable in on js fiile, is this available to another js file that is loaded?

    Answer: yes, it's available (unless inside of a function or something)

    BUT: does it matter WHERE I declare this variable??

    Let's say, I have the following in my HTML file:

    <script src="script/main.js"></script>

    And then at the very bottom of my HTML page before the </body> tag I have:

    <script type="text/javascript">var myvariable = "Sample Data";</script>

    So, is myvariable available to main.js?

    In simple tests I've done, yes it does seem to be available

    But... what if I have a lot of other js code running in between
    Does this change anything?

    Thanks


    OM

  2. #2
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    Quote Originally Posted by OM2 View Post
    Let's say, I have the following in my HTML file:

    <script src="script/main.js"></script>

    And then at the very bottom of my HTML page before the </body> tag I have:

    <script type="text/javascript">var myvariable = "Sample Data";</script>

    So, is myvariable available to main.js?

    In simple tests I've done, yes it does seem to be available
    If you're setting the variable after loading main.js, then no, it won't be immediately available. If it's working in your tests, I'm guessing your code waits for the DOM ready event before running.


    Quote Originally Posted by OM2 View Post
    But... what if I have a lot of other js code running in between
    Does this change anything?
    There's always the possibility that one of the scripts will overwrite your global variable. That's one of the problems with global variables.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  3. #3
    Join Date
    Apr 2005
    Posts
    493
    Quote Originally Posted by Jeff Mott View Post
    If you're setting the variable after loading main.js, then no, it won't be immediately available. If it's working in your tests, I'm guessing your code waits for the DOM ready event before running.




    There's always the possibility that one of the scripts will overwrite your global variable. That's one of the problems with global variables.
    jeff, thanks for the reply
    that's actually an amazing help
    yes: someone else said to me that the dom waits for the ready event
    the application is actually for jquerymobile

    there'll be no chance of the code being over written - it's just got a unique name and a one off usage

    question: let's say there is a lot of other js code that is loaded and run - is there ANY danger that the code might get executed BEFORE the dom loads? this is my concern

    thanks

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,674
    depends on what is in the code

Thread Information

Users Browsing this Thread

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

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