DEV Community

Cover image for How to call javascript file in html?

How to call javascript file in html?

joshuabrown0010 profile image joshua-brown0010 ・1 min read

Like Cascading Style Sheets - CSS (which allows the style to be embedded within the document with the style tag), JavaScript code can be embedded within an html document using the script tag.

<script type = "text / javascript">
// Code
Enter fullscreen mode Exit fullscreen mode

The code can also be found in a separate document (with a js extension) that is called to include the functionality within the HTML document.

To include an external style sheet, the (link) tag is used. In JavaScript the same tag (script) is used but the src attribute is added with reference to the file to be added.

The reference to the file can be a relative path - that the file is in the same directory where the html is or in a folder within the same domain - or it can be an absolute path to a resource - /mi-code.js


If you call a file using http: // but the page is viewed through https: // a warning will be generated (that you want to include non-secure content on a secure page). To avoid this, two diagonals are simply used and the protocol is not specified.

// File within the domain of the page / site
<script type = "text / javascript" src = "file.js"> </script>
// External file, the // are used so that the page
// can be viewed via http or https
<script type = "text / javascript" src = "//"> </script>
Enter fullscreen mode Exit fullscreen mode

Read more

Discussion (0)

Editor guide