AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

HTML script tag: how to integrate scripts into HTML?

PARTAGEZ

Using HTML

html

If you write the JavaScript code directly in the element, you don't need any other parameters. If you are referring to an external script, you must also insert the HTML attribute src (for English source). Syntax for HTML

html

Advice

It is recommended to include a alternative text which will be displayed if the script cannot be executed, for example, if a user has disabled JavaScript. For this you can use the element

Example of integrating an external script

We transpose this example to the alternative possibility of integrating JavaScript code as an external script. The code of clickable buttonwhich displays the message “You clicked the button!” » after a click, is saved in a new file named script.js :

// script.js
document.addEventListener('DOMContentLoaded', function() {
    document.getElementById('monbouton').addEventListener('click', function() {
        alert('Vous avez cliqué sur le bouton !')
    });
});

javascript

In the second step, we embed the script into an HTML document which is located in the same directory that the script:

What are the attributes of the HTML tag