-1

I am learning JavaScript.

I've written this code, but it does not seem to run.

<!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 content="text/html; charset=utf-8" http-equiv="Content-Type" />
<script language="javascript" type="text/javascript">
    function nav(){
        try{
            var str="<table border=1>";
            for (var n in navigator){
                str+="<tr><td>" + n + "</td></tr>";
                }
            str+="</table>
            return("jetzt wird das Ding aufgelistet: <br/>" + str);

        }catch(e){return(e);}

    }
    function writeit(){
    document.write(nav());
    }

</script>
<title>for learning purpos</title>
</head>

<body>
<form>
<script>
    document.write(nav());
</script>

<p>press the button to get the properties in navigator</p>
<input type="button" id="btnNavigator" value="get" onclick="writeit();"/>

</form>
</body>

</html>
CollinD
  • 5,885
  • 2
  • 18
  • 38

2 Answers2

2

You haven't closed one string.

 str+="</table>";
0

Best practice is to look into the console from developer tab, inspect page. Also on that tab on the bottom, you can try javascript code or even jQuery if you have the library added.

Your code it's wrong at line 13 you have Invalid or unexpected token because you didn't close the string and at line 30 nav it's not defined.

You can use this code:

    function navBuild(n) 
    {
        var tblS = "<table>";
        var tblE = "</table>";
        var strTrTd = "";
           for (i=1; i<=n; i++)
           {
             strTrTd = strTrTd + "<tr><td>" + i + "</td></tr>";
           }

        var strAll = tblS + strTrTd + tblE;

        console.log(strAll);
        document.getElementById("contentBox").innerHTML = strAll;

    }

And in your HTML you could use:

<input type="button" id="btnNavigator" value="get" onclick="navBuild(5);"/>
<div id="contentBox"></div>
Philipos D.
  • 480
  • 1
  • 7
  • 14