To include Javascript in an HTML page use the tag <script type="text/javascript"> seu script </script>
. Anything within these tags will be interpreted as Javascript language. HTML5 suffices <script></script>
.
If you include inside the tags <head></head>
, the script will be executed before the page is displayed (i.e., if you do not load the script, the page does not open, but when it opens, it already opens with Javascrit working 100%), and if you include it within the <body></body>
will be executed during the exhibition.
You can also include Javascript through an external file, linking it between tags <head>
:
<script src="_javascript/meu_script.js"></script>
An example of using Javascript to manipulate a DOM element (the script opens a div according to the choice of a button radio
):
var esconderradio = function(var1, var2) {
if (var1) {
document.getElementById(var2).style.display = "";
}
else {
document.getElementById(var2).style.display = "none";
}
};
<label class="radio" for="Csaldsim">
Sim
<input type="radio" id="Csaldsim" name="radio" onclick="esconderradio(true, 'salvenc')"></label>
<label class="radio" for="Csaldnao" onclick="esconderradio(false, 'salvenc')">
Não
<input type="radio" name="radio" id="Csaldnao" onclick="onclick="esconderradio('Csaldim', 'salvenc')"></label>
<br>
<div class=" col-md-3 panel panel-default" id="salvenc" style="display:none;">
<label class="btn" for="Csalvenc">
<input type="number" class="form-control" name="Tsalvenc" id="Csalvenc"></label>
</div>
To study Javascript I suggest the codeacademy.
You mean how to include javascript in HTML? The place where scripts should be included in the HTML page?
– gustavox
What you really need to do ?
– AndreeH
Your question is not clear, try to elaborate it better.
– Felipe Avelar