Next-Gen App & Browser
Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud

How to include JavaScript in HTML?

JavaScript or JS is a client-side scripting language to build modern and interactive web applications. As JavaScript is not standalone, you must rely on HTML to run your software programs.

There are three ways to include JavaScript in your HTML pages.

Embedding code: To include JavaScript code in your HTML pages, use the <script>.....</script> HTML tag containing JavaScript code. You can add the JavaScript code in the <body> or <head> tag.

Example:


<!DOCTYPE html >  
<html>  
<head>  
<title>LambdaTest</title>  
<script>  
document.write("A Test Orchestration & Execution Platform");  
</script>  
</head>  
<body>  
<p>Welcome to LambdaTest!</p>  
</body>  
</html>

Inline code: This method calls a function in the HTML event attributes without using the <script>.... </script> tag.

Example:


<!DOCTYPE html >  
<html>  
<head>  
<title>LambdaTest</title>  
</head>  
<body>  
<p>  
<a href="#" onClick="alert('Want to test?');">Register here</a>  
</p>  
<p> Welcome to LambdaTest </p>  
</body>  
</html>  

External file: It involves creating a JavaScript file to hold the code of JavaScript explicitly. After that, add that JavaScript file to your HTML document using the src attribute of the HTML <script> tag.

Example:


<html>  
<head>  
<meta charset="utf-8">  
<title>LambdaTest</title>  
</head>  
<body>  
<form>  
<input type="button" value="Register" onclick="display()"/>  
</form>  
<script src="index.js">  
</script>  
</body>  
</html>    

Test your website on 3000+ browsers

Get 100 minutes of automation test minutes FREE!!

Test Now...

KaneAI - Testing Assistant

World’s first AI-Native E2E testing agent.

...
ShadowLT Logo

Start your journey with LambdaTest

Get 100 minutes of automation test minutes FREE!!

Signup for free