www.webdeveloper.com
Results 1 to 3 of 3

Thread: How input an object word into a variable?

  1. #1
    Join Date
    Mar 2014
    Posts
    2

    How input an object word into a variable?

    I am trying to make this script working with a prompt that ask the user which object (screen, location, window) property will show.
    If I use this code it will works with this:

    Propriedades

    Propriedad Valor
    availWidth = 1280
    availHeight = 800
    availTop = 0
    availLeft = 0
    pixelDepth = 24
    colorDepth = 24
    width = 1280
    height = 800

    Code:
    <body>
    <h2>Propriedades</h2>
    <table id="tabla">
    <tr><th> Propriedad </th><th> Valor </th></tr>
    </table>
    
    <script type="text/javascript">
    
    
    
    var i, tabla = document.getElementById("tabla");
    for (i in screen){
        tabla.innerHTML += "<tr><td>"+i+" </td><td> = " +screen[i]+ "</td></tr>";
    }
    </script>
    
    </body>

    But If I ask which name with a prompt, then the output is wrong, why? Please Help me! Thanks.


    Propriedades

    Propriedad Valor
    0 = s
    1 = c
    2 = r
    3 = e
    4 = e
    5 = n

    Code:
    <body>
    <h2>Propriedades</h2>
    <table id="tabla">
    <tr><th> Propriedad </th><th> Valor </th></tr>
    </table>
    
    <script type="text/javascript">
    
    name=prompt("¿Que propriedad quiere?");
    
    var i, tabla = document.getElementById("tabla");
    for (i in name){
        tabla.innerHTML += "<tr><td>"+i+" </td><td> = " +name[i]+ "</td></tr>";
    }
    </script>
    
    </body>

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    Code:
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8" />
    <title>abc</title>
    </head>
    <body>
    <h2>Propriedades</h2>
    <table id="tabla">
    <tr><th> Propriedad </th><th> Valor </th></tr>
    </table>
    
    <script type="text/javascript">
    (function(){
    var name=prompt("?Que propriedad quiere?"),
        i,
        tabla = document.getElementById("tabla");
    try{
    var x=eval(name);
    for(i in x){
    tabla.innerHTML += "<tr><td>"+i+" </td><td> = " +x[i]+ "</td></tr>";
    }
    }
    catch(e){tabla.innerHTML += "<tr><td colspan='2'><h1 style='color:Crimson'>wrong request</h></td></tr>";}
    })();
    </script>
    </body>
    </html>
    Last edited by Padonak; 03-21-2014 at 01:23 PM.
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  3. #3
    Join Date
    Mar 2014
    Posts
    2
    Thanks a lot Padonak.

    Meanwhile I've found that also this works:

    Code:
    <script type="text/javascript">
    
    var name=prompt("¿De que Objeto quiere ver las propriedades?");
    
    var obj = window[name];
    
    if (typeof obj != "object")
    document.writeln("El Objeto \""+ name+"\" no existe");
    
    else
    
    var tit = document.getElementById("nome");
    	tit.innerHTML = "Propriedad del Objeto \""+ name+"\"<br><br>";
    
    var i, tabla = document.getElementById("tabla");
    
    for (i in obj) {
    	tabla.innerHTML += "<tr><td>"+i+" </td><td> =  " +obj[i]+ "</td></tr>";
    }
    
    function reloadPage()
      {
      location.reload();
      }
    </script>

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