how to get defined variables in js script

Discussion in 'JavaScript' started by spawn, Jan 5, 2007.

  1. #1
    hello
    i want to make a script that is user configurable.
    so the user can define his own variables.
    how can i find what are the variables he defined?

    something like this:
    script1.js
    // zone for defining the vars
    var user_var1=1;
    var user_var2=2;

    // my function

    function show_vars() {

    foreach ( defined variables by user )
    alert( variable name );

    }

    i hope i'm clear enough
    thanks
     
    spawn, Jan 5, 2007 IP
  2. smallbuzz

    smallbuzz Peon

    Messages:
    125
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What's your purpose for doing this? If you don't know what variables the user is going to define, how are you going to know what to do with them?
     
    smallbuzz, Jan 5, 2007 IP
  3. spawn

    spawn Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i want to make a tool for validating forms
    so the user can set their "type" of input with his own properties to validate

    like this
    <input type="text" rel="name" />
    so he set
    var min_name=5

    and the js makes the validation for minimum 5 chars in that input
    i know it might seem stupid but i want to make this script unobtrusive and EASY configurable for any form.
    all you have to add is a rel for manatory fields and in rel the kind of validation defined or customized by you.
    what do you think?
     
    spawn, Jan 5, 2007 IP
  4. Young Twig

    Young Twig Peon

    Messages:
    27
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Make an object. I think you can iterate those like

    
    var obj = new Object();
    obj["asdf2"] = "WTF";
    obj["asdfef4"] = "WTF";
    obj["asdf34333"] = "WTF";
    
    for(var i in obj)
    {
         alert(obj[i]);
    }
    
    Code (markup):
     
    Young Twig, Jan 5, 2007 IP
  5. smallbuzz

    smallbuzz Peon

    Messages:
    125
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The HTML quickform package at http://pear.php.net writes server side and client side validation for you. It sounds like what you are trying to do. You can look at their code I guess.
     
    smallbuzz, Jan 9, 2007 IP