Jump to content

porque não inicia o JavaScript antes de mostrar o conteúdo WEB?


Rúben Periquito Developer

Recommended Posts

<!DOCTYPE html>
<html>
<head>
<title></title>


</script>
</head> 
<body>

    <h1>Title News</h1>
    <h2>These are today's news</h2>
    
    <div id="news1">Today's news are nothing special. Please come back tomorrow...</div>
    
<script type="text/javascript">    

var firstelement = document.getElementById("news1");

alert(firstelement);
    
    
</body>
</html>

Rúben Periquito 

Link to comment
Share on other sites

1 hora atrás, Zex disse:

O " </script> " está em cima em vez de estar depois do "alert"

e aqui? 

<!DOCTYPE html>
<html>
<head>
<title></title>


</head> 
<body>

    <h1>Title News</h1>
    <h2>These are today's news</h2>
    
    <div id="news1">Today's news are nothing special. Please come back tomorrow...</div>
    
<script type="text/javascript">    

var parent = document.childNodes[0].childNodes[0];

alert(parent.nodeName);

</script>    
</body>
</html>

porque não aparece um alerta de Javascript com o nome do "filho"?

Rúben Periquito 

Link to comment
Share on other sites

A página é enviada ao browser e só quando este a recebe toda é que a começa a interpretar.
Só depois de estar estar pronta é que o Javascript é executado, tem a ver com o ciclo de vida do DOM.
Podes ler mais aqui:

10 REM Generation 48K!
20 INPUT "URL:", A$
30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50
40 PRINT "404 Not Found"
50 PRINT "./M6 @ Portugal a Programar."

 

Link to comment
Share on other sites

o javascript funciona em qualquer lado 

 <!DOCTYPE html>
<html>
<head>
<title></title>


<script> 
var parent = document.childNodes[0];
alert(parent.nodeName+1);
</script>


<script type="text/javascript">

function run()
{    
var parent = document.childNodes[0];
alert(parent.nodeName+2);
}

</script> 

</head> 

<body onload="run()">

    <h1>Title News</h1>
    <h2>These are today's news</h2>
    
    <div id="news1">Today's news are nothing special. Please come back tomorrow...</div>
    
<script> 
var parent = document.childNodes[0];
alert(parent.nodeName+3);
</script>
 
  
</body>

<script> 
var parent = document.childNodes[0];
alert(parent.nodeName+4);
</script>
 
</html> 

<script> 
var parent = document.childNodes[0];
alert(parent.nodeName+5);
</script>
 
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.