www.webdeveloper.com
Results 1 to 13 of 13

Thread: how to determine if a *dynamic* var is undefined?

  1. #1
    Join Date
    Apr 2009
    Posts
    77

    how to determine if a *dynamic* var is undefined?

    Hi All

    I am trying to run some code if a dynamic var is not yet defined.. and so tried a bunch of failed lines, plus this:

    Code:
    if (typeof eval("myDynamicVarName" + aDynamicNumber) == 'undefined') {
    doSomething();
    ...but instead of running
    doSomething();,
    it gives this error (FF3/mac):
    "myDynamicVarName1 is not defined"

    Right. But how do I tell the code that this case is not an error, but something I want to act on?

    Thanks for any help!
    -Govinda

  2. #2
    Join Date
    Oct 2007
    Location
    Western Massachusetts, USA
    Posts
    387
    This works:
    PHP Code:
    <script type="text/javascript">
    function 
    doSomething() { document.write("<p>Success!</p>\n"); }
    0;
    eval(
    "dyn"+n+" = \"a\";");
      if (
    typeof(window["dyn"+n]) != "undefined") { doSomething(); }
      if (eval(
    "typeof(dyn"+n+") != \"undefined\"")) { doSomething(); }
    </script> 
    Last edited by savvykms; 08-29-2010 at 08:27 PM.

  3. #3
    Join Date
    Apr 2009
    Posts
    77
    I just tried that but get the same (error/failing) result..
    "myDynamicVarName1 is not defined".

    Do you have any other suggestions?
    Thanks!
    -Govinda

  4. #4
    Join Date
    Oct 2007
    Location
    Western Massachusetts, USA
    Posts
    387
    Just edited my post, what i did was stupid. I always forgot proper checking in javascript. Good thing I used to use eval() all the time.

    Here it is:
    Quote Originally Posted by savvykms View Post
    This works:
    PHP Code:
    <script type="text/javascript">
    function 
    doSomething() { document.write("<p>Success!</p>\n"); }
    0;
    eval(
    "dyn"+n+" = \"a\";");
      if (
    typeof(window["dyn"+n]) != "undefined") { doSomething(); }
      if (eval(
    "typeof(dyn"+n+") != \"undefined\"")) { doSomething(); }
    </script> 
    Last edited by savvykms; 08-29-2010 at 08:44 PM.

  5. #5
    Join Date
    Jan 2009
    Posts
    3,346
    This sounds like a good case for an array rather than multiple eval statements.

  6. #6
    Join Date
    Mar 2010
    Location
    Singapore
    Posts
    367
    Quote Originally Posted by wdgovinda View Post
    I just tried that but get the same (error/failing) result..
    "myDynamicVarName1 is not defined".

    Do you have any other suggestions?
    Thanks!
    -Govinda
    Below is sample code from w3schools. Note that is no double-quote or single-quote surrounding undefined. I believe undefined is a special property.

    Code:
    <script type="text/javascript">
    
    var t1="";
    var t2;
    
    if (t1==undefined)
    {
    document.write("t1 is undefined");
    }
    if (t2==undefined)
    {
    document.write("t2 is undefined");
    }
    </script>

  7. #7
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    if ( (typeof aDynamicNumber && typeof window["myDynamicVarName" + aDynamicNumber]) == 'undefined') {

  8. #8
    Join Date
    Apr 2009
    Posts
    77
    sohguanh,
    thanks, but I need it for a *dynamic* var.. and that was the hard part for me.

    Fang & savvykms,
    those work great, thank you!

    criterion9,
    I was not sure how to implement an array for this problem because I do not know in advance the dynamic number part of the dynamic var. Server-side code generates a list and I need one dynamic var per item in the list... but it is only the client at runtime who will know how many items should go into that array. Maybe you are saying I should just look to see if the item exists in the array.. but I ended up not trying to go that route since the other posts here solved the issue.

    Thanks everyone!
    -Govinda

  9. #9
    Join Date
    Jan 2009
    Posts
    3,346
    To find the total number of items in an array use ArrayVar.length(). Usually that is a much cleaner way to go rather than testing to see if a series of variables exist that all end with a numeric value (especially true if the numbers are sequential).

  10. #10
    Join Date
    Apr 2009
    Posts
    77
    criterion9,

    that makes sense.. but in this case the question would not be how many items are in the array, but IF the dynamic var had been initialized yet. I was not initializing them onLoad.. I was only initializing upon the first time the user clicked an anchor.. so all the vars were un-initialized at first.. I needed a way to test whether the anchor in question had been clicked yet.. (because after they clicked the anchor then from then on the var was initialized.)

    Would you still favor an array in this case?

    -Govinda

  11. #11
    Join Date
    Jan 2009
    Posts
    3,346
    I would still favor an array just for ease of reading/editing later. You can still say you have x number of elements you wish to track in that way and check if they are initialized later.

    Personally I would initialize them first, especially if you know or can find out how many elements you'll be identifying in this way at or before page load.

  12. #12
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    just use an object instead of window. that way you can ask if(obj["dyn"+x]){ and not throwing an exception if missing or using an eval later.

  13. #13
    Join Date
    Oct 2007
    Location
    Western Massachusetts, USA
    Posts
    387
    I noticed an array could probably be used for this. The question was about dynamicly created variables through eval() statements, rather than arrays. As far as linking a server-side list/array into a client-side script, try generating the script via PHP or another server-side langauge or CGI setup.

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