SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    passing object name as parameter

    Greetings,I'm trying to write a generic isNullEmpty function that takes an object name as a parameter and checks if it exisits and/or if its value is empty. how can I check the existance of an object name when it's passed in as a function parameter? the code below does NOT work. Thanks.
    PHP Code:
    function isNullEmpty(objName)
    {
       if (eval(
    objName))    
       {
          return (eval(
    objName) == '');  //eval() is not working.
        
    }
        return 
    true;


  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's best to avoid eval(). It's slow and there is usually an alternative.

    What exactly are you passing to the function? A string?
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    kravvitz, yes, I'm passing the name of the object as a string.

  4. #4
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like Kravvitz said, using eval is in most cases bad practice. Why not just do a null check?

  5. #5
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is the object always global? Or are might you pass something like "object1.object2"?
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  6. #6
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, for now, I'm thinking it might be global, but if we can get it to check on something like object1.object2, that would be sweet.

  7. #7
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is the use-case for this? It sounds like you got something completely backwards?

  8. #8
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    kyberfabrikken, visual basic has a isNullEmpty property for visual basic strings. my thought is to write this function where I can pass in a string, which can be an object name, or even an element ID and I can check whether that elment exisits document.getElementByID('passedinID') and get it's value to see if it's empty. also I want to use this function to passin global object names and simply check if they exist and/or if their values are empty. I hope this gives you an idea about what I'm trying to do here. Thanks.

  9. #9
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Again, just do a null check or check for undefined... Creating a function for this check seems a bit of unnecessary overkill.

  10. #10
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Pepejeria, I guess I see your point in this. but going back to my original question, for a future reference, I would like to know how to execute variable names/object names when they're passed in as a parameter. Thanks.

  11. #11
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cbiti View Post
    kyberfabrikken, visual basic has a isNullEmpty property for visual basic strings. my thought is to write this function where I can pass in a string, which can be an object name, or even an element ID and I can check whether that elment exisits document.getElementByID('passedinID') and get it's value to see if it's empty. also I want to use this function to passin global object names and simply check if they exist and/or if their values are empty. I hope this gives you an idea about what I'm trying to do here. Thanks.
    Not knowing vb, I would guess that isNullEmpty() checks if the value of a variable is null or the variable is undefined. You can implement this by checking if the typeof operator returns "undefined" or the value equals null:
    Code:
    isUndefinedOrNull = function(o) {
      return typeof(o) == 'undefined' || o === null;
    }
    Combining the function with a check for the existence of an element in the document, would be a bad idea IMHO. Variables in javascript and elements in the DOM are two different things, even if they are tightly integrated. You can create a wrapper function over the horridly longish document.getElementById(), to save the typing though. Most people use $ as the name for such a function - an idea initially coming from the prototype-framework. An implementation could simply be:
    Code:
    $ = function(id) {
      return document.getElementById(id);
    }
    You can then combine the two functions in one expression, to check if a given element exists in your document:
    Code:
    if (isUndefinedOrNull($("foo"))) {
      alert("no foo");
    } else {
      alert("there's a foo, alright");
    }


Bookmarks

Posting Permissions

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