0

i wonder why my code says that it's not defined when i'm trying to do a simple code with data binding :/

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<object name="login" id="login" classid="CLSID:333C7BC4-460F-11D0-BC04-0080C7055A83">
    <param name="DataURL" value="member.txt"/>
    <param name="UseHeader" value="true"/>
    <param name="TextQualifier" value=""/>
    <param name="FieldDelim" value="|"/>
</object>

    <script>
        var rs = login.resultset;
        function validation()
        {
            rs.moveFirst();
            while(rs.moveNext())
            {           
                if(document.getElementById("txtid")== rs(0) && document.getElementById("txtpass")==rs(1))
                {
                    alert("Login Succeed");
                    return; 
                }
            }
            alert("Email or Password Wrong");
            return;
        }
    </script>

</head>


<body>
<form>
Username: <input type="text" id="txtid" /> <br/>
Password: <input type="text" id="txtpass" /><br/>
<input type="submit" value="game start" id="btnstart" onclick="validation()"/>
</form>
</body>
</html>

the error: login is not defined

but i know that it's defined ! i have tried to search about this but i got no clue about what's wrong in my code :/ help please?



EDIT: i've updated my code to something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>


<body>


<form>
Username: <input type="text" id="txtid" /> <br/>
Password: <input type="text" id="txtpass" /><br/>
<input type="submit" value="game start" id="btnstart" onclick="validation()"/>
</form>



<object name="login" id="login" classid="CLSID:333C7BC4-460F-11D0-BC04-0080C7055A83">
        <param name="DataURL" value="member.txt"/>
        <param name="UseHeader" value="true"/>
        <param name="TextQualifier" value=""/>
        <param name="FieldDelim" value="|"/>
</object>

<script>
    var login = document.getElementById('login');
    var rs = login.resultset;
    function validation()
    {
        rs.moveFirst();
        while(rs.moveNext())
        {           
            if(document.getElementById("txtid")== rs(0) && document.getElementById("txtpass")==rs(1))
            {
                alert("Login Succeed");
                return; 
            }
        }
        alert("Email or Password Wrong");
        return;
    }
</script>

</body>
</html>

the next error i got is the rs is undefined when i'm clicking the button. am i doing something wrong?

aquatorrent
  • 801
  • 2
  • 8
  • 11

2 Answers2

1

In your code:

> <object name="login" id="login" ...>
>   ...
> </object>
>  <script>
>          var rs = login.resultset;  

You are expecting an element with id login to be made available as a global variable, which it is some browsers always and others only under certain conditions. It was never a good idea and should never be used, always reference elements using standard DOM methods, in this case getElementById:

  var el = document.getElementById('login');

Futher, there is no standard resultset attribute for object elements and you haven't defined one, therefore it is not reasonable to expect that the DOM element returned by the above expression will have a resultset property. At the very least, before attempting to use the rs variable, you should test that it has a value suitable for what you intend using it for, e.g.

    if (rs && rs.moveFirst) {
      rs.moveFirst(); 

and so on.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • i've updated my code using `.getElementById` and got another error when clicking the submit button . . – aquatorrent Apr 17 '12 at 05:41
  • @aquatorrent — That would be a different problem and one we couldn't answer without knowing that the error was (so ask a new question detailing it). – Quentin Apr 17 '12 at 06:17
  • @Quentin: is that so? o.o but i think that the error is the same as the first one.. >.> – aquatorrent Apr 17 '12 at 06:23
  • @aquatorrent — No, the first one was you failing to get the object. This one is that the object doesn't have a resultset property (which RobG's answer has already covered anyway. – Quentin Apr 17 '12 at 06:35
  • oh, i see. i'll ask a new one then. – aquatorrent Apr 17 '12 at 06:59
0
  • Content should be placed in the <body> and not in <head>. The only things that are reasonable to be placed in the <head> are scripts (the ones that don't manipulate the DOM at once), styles and misc. meta-data about your page. All the rest that appears in the page should be in <body>

  • Although I recently knew that id'ed elements expose a global variable of the same name as the id and refers to the element it IDs, you should not refer to the element in that way. You should do something like document.getElementById() to be safe.

  • And for safe script execution, place <script> tags after your content but before you close the <body> element. this ensure that all elements that your scripts refer to are already existing.


<!DOCTYPE html>
<html>
    <head>
       <!-- title, meta, styles, non-DOM scripts up here -->
    </head>
    <body>

        <!-- everything that appears in the page here -->

        <object id="login">
           <!-- ... --->
        </object>

        <script>
            //after the content, DOM manipulation scripts go here

            var login = document.getElementById('login');

            //now "login" is the object element

        </script>

    </body>
</html>
RobG
  • 142,382
  • 31
  • 172
  • 209
Joseph
  • 117,725
  • 30
  • 181
  • 234
  • i moved the `` and ` – aquatorrent Apr 17 '12 at 05:23
  • i'm doing exactly as you said and got another error. i've updated my question about it :/ – aquatorrent Apr 17 '12 at 05:37
  • what is `resultset`? where does this come from? – Joseph Apr 17 '12 at 05:42
  • i think it comes from the `` tag. it reads data from a `.txt` file, as far as i know – aquatorrent Apr 17 '12 at 05:43
  • if you are trying to read the `txt` file using js, here are two articles that mention HTML5 FileReader: [here](http://www.htmlgoodies.com/beyond/javascript/read-text-files-using-the-javascript-filereader.html#fbid=UXE07FsO0Qu) and [here](http://stackoverflow.com/q/4950567/575527) – Joseph Apr 17 '12 at 05:54
  • the problem is that i was told to use ActiveXObject when reading the file :/ – aquatorrent Apr 17 '12 at 05:59
  • then your instructor is an MS fanboy. there are other ways to read a file, like AJAX and FileReader. if ActiveX is your homework, then [this might help](http://css.dzone.com/tips/javascript-how-read-and-write) – Joseph Apr 17 '12 at 06:03
  • i've read it and now i'm confused about how am i supposed to split the file. the `.txt` file is like : `sad|tryrty` where i have to put `sad` and `tryrty` to different variables for checking :/ and seems i can't find the end of file function >.> – aquatorrent Apr 17 '12 at 06:22