HTML <script>
query_builder | Christopher PisaniThe HTML element <script> is used for the server-side client scripting language. The element can be used to write scripting language between the tags or point to an external javascript file. Javascript language is very convenient to manipulate information and validate forms dynamically without the need to refresh a web page, thus making a smoother user experience.
A simple javascript data manipulation with a click of a button
<script>
function myFunction() {
document.getElementById('example').innerHTML = 'This text has been changed dinamically with a click of a button.';
}
</script>
<div id="example">Some static text here</div>
<button onclick="myFunction()">Click here to change Text</button>
Changing HTML styled elements dynamically
<script>
function myFunction() {
document.getElementById('example').style.background = "red";
}
</script>
<div id="example" style="background:yellow;">
Some static text here
</div>
<button onclick="myFunction()">Click here to change Style</button>
Display a custom popup message
<script>
function myFunction() {
alert('This is a test message');
}
</script>
<button onclick="myFunction()">Click Here</button>
Button clicks count
<script>
function myFunction() {
var example = document.getElementById('example');
example.innerHTML = parseInt(example.innerHTML) + 1;
}
</script>
Button Clicks: <span id="example">0</span>
<button onclick="myFunction()">Add Click</button>
Available Attributes
Attribute | Values | Functionality |
---|---|---|
async | External file loads while the page is being loaded, without waiting for scripting file to fully load before loading the next document code (Asynchronously execution) | |
charset | charset code | The character encoding used in an external scripting file |
defer | External script file is executed only when the page is fully loaded. | |
src | linked file | The source of the scripting file |
type | application/javascript (default) Look at IANA Media Types for a complete list |
The type of scripting language |