My first Javascript code does not work. And now?

-2

I'm trying to run my first javascript. But by clicking on the button I created, the text that should appear on the screen does not appear. Where am I going wrong? My browser is updated so it should run Javascript 1.8.5. Even though I tried to set up version 1.1 and it does not work the same ...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 5.0//EN"
"http://www.w3.org/TR/REC-html40/strict.dtd">
<HTML>
<HEAD> 
<TITLE>Menu</TITLE>
    <script language = "JavaScript1.8.5">
        function teste1(){
            document.write("<h1>Teste1 foi acionado!!!</h1>")
            var teste = new Object();
            teste.x = 2,4;
            teste.y = 5;
            document.write(teste.x)
    </script>
</HEAD> 
<BODY>
    <input type="button" value="Teste 1" onClick="teste1()">
</BODY>
</HTML>
    
asked by anonymous 10.03.2018 / 21:55

2 answers

1
document.body.innerHTML += "<h1>Teste1 foi acionado!!!</h1> "; 

The missing date is missing.

And the script stays in the Body

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 5.0//EN"
"http://www.w3.org/TR/REC-html40/strict.dtd">
<HTML>
<HEAD> 
<TITLE>Menu</TITLE>
</HEAD> 
<BODY>
    <input type="button" value="Teste 1" onClick="teste1()">
    <script>
        function teste1(){
            document.body.innerHTML += "<h1>Teste1 foi acionado!!!</h1>";
            var teste = new Object();
            teste.x = 2,4;
            teste.y = 5;
            document.body.innerHTML += teste.x;
        }
    </script>
</BODY>
</HTML>
    
10.03.2018 / 22:00
3

The language attribute is no longer used for a long time ( see this answer ). Only use:

<script>
    // código
<script>

The JavaScript version is defined by the browser you use. If you use a current browser, it will run the latest JavaScript. There is no need to set a version in the tag, because it will return an error.

In addition to that, you closed the function with the closing key } .

    
10.03.2018 / 22:08