My first Javascript code doesn’t work. Now what?

Asked

Viewed 102 times

-3

I’m trying to run my first javascript. But when I click the button I created, the text that should appear on the screen does not appear. Where am I missing? My browser is updated so it should run Javascript 1.8.5. Even so I tried even setting version 1.1 and it doesn’t 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>

2 answers

3

The attribute is no longer used language a long time ago (see this response). Use only:

<script>
    // código
<script>

The Javascript version is set by the browser you use. If you use a current browser, it will run the most modern Javascript. No need to set a version in the tag, because it will return error.

In addition to the missing close function with the lock key }.

1


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

Missing the quote button.

And the script goes on 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>
  • It worked! But it seems that the script never stops running, because the browser page is "loading" forever. As you can see, I put the </script> at the end...

  • Should I disagree about the statement right at the beginning of the answer? I see no lack of quotation marks, nor in the history of the question. I saw other adjustments in the code and what I realized helped actually (I may be wrong) was the key lock function }, as the dvd put in the other answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.