Embed JavaScript in HTML - Here's how
You can easily integrate external JavaScript into HTML. We show you the different options here.
Embed JavaScript directly on HTML pages
It is best to keep the script in the head so that your page and other scripts are loaded as quickly as possible.
- To insert JavaScript in HTML 5, the entry between these tags is placed in the "XXXX" XXXX area.
- If you are using an older HTML version, the script looks like this: XXXX.
- For visitors to your site who have not activated JavaScript, you can use "noscript" to specify a text that is displayed instead of the JavaScript.
- The entire command then looks like this: XXXXPlease activate JavaScript to open the page
- If the script is required on other HTML pages, you must enter it in the corresponding individual pages and make changes on each individual page.
Embed JavaScript as an external file in HTML pages
If you have the script in a file with the extension .js, you can embed it in the header area of your page in the following way:
- For HTML5 this is enough:.
- For an older HTML version, use this variant:.
- Transfer the .js file to your web server using an FTP program.
- The great advantage of a JavaScript file is that all pages can access the JavaScript functions as soon as the link has been inserted in the head of the respective website.
- Changes are then only necessary in the .js file and no longer on every single page.
In the next practical tip we will explain how you can integrate JavaScript into WordPress.