HTML Script

Bookmark
Learn about using client side scripts like JavaScript in a HTML document.

To include client side script in HTML documents, <script> tag can be used. Client side scripts brings in the dynamic flavor to static HTML pages. JavaScript is the most popular client side language.

HTML Script Tag

Using HTML script tag client side scripts can be included. Script can be included in two ways, either the script code itself can be enclosed within the <script> tag in the HTML page itself or using src attribute an external script file can be included.

Very common use case for including script in client side is form validation and dynamic change in display elements. Following is an example.

<!DOCTYPE html>
      <html>
      <head>
      <title>HTML Meta Tag Example</title>      
<script type="text/javascript" src="/js/script.js"></script>
<script type="text/javascript"> function helloScript(){    alert("Hello, World"); } </script>
</head>
<body> <p>Click button to say hello!</p> <input type="button" onclick="helloScript();" name="hello" value="hello"  /> </body>
</html>

Example Output

HTML Meta Tag Example

Click button to say hello!

In the above example, we have included script code in HTML head within the <script> tag. Then we have also included an external file using src attribute.

HTML noscript Tag

Users can disable the support in web browsers for rendering scripts like JavaScript. On encountering such situation <noscript> tag will be executed. This noscript tag can contain any HTML elements. It will be executed and not other processing will be done when the browser does not support scripts or if it is disabled.

<head>
<script type="text/javascript">
function helloScript(){
   alert("Hello, World");
}
</script>

<noscript>Your browser does not support JavaScript!</noscript>

</head>

Some More JavaScript Examples

<script>
document.getElementById("hello").innerHTML = "Hello World!";
</script>
<script>
document.getElementById("hello").style.fontSize = "12px";
</script>

Exception Occured:

TypeErrorException
Messageerror_log(app.log): Failed to open stream: Permission denied
File/home/dh_czz6eb/tutorialwalk.com/common/error.php
Line36
Trace#0 (): log_error(2, 'error_log(app.log): Failed to open stream: Permission denied', '/home/dh_czz6eb/tutorialwalk.com/common/error.php', 36)
#1 /home/dh_czz6eb/tutorialwalk.com/common/error.php(36): error_log('27-05-2024 02:35:29 am (IST),/home/dh_czz6eb/tutorialwalk.com/common/common.php,193,0,ErrorException,Optional parameter $per_page declared before required parameter $href is implicitly treated as a required parameter,/html/html-script.html,#0 /home/dh_czz6eb/tutorialwalk.com/common/error.php(115): log_error(8192, 'Optional parame...', '/home/dh_czz6eb...', 193)
#1 [internal function]: check_for_fatal()
#2 {main}
', 3, 'app.log')
#2 /home/dh_czz6eb/tutorialwalk.com/common/error.php(8): log_exception(ErrorException)
#3 /home/dh_czz6eb/tutorialwalk.com/common/error.php(115): log_error(8192, 'Optional parameter $per_page declared before required parameter $href is implicitly treated as a required parameter', '/home/dh_czz6eb/tutorialwalk.com/common/common.php', 193)
#4 (): check_for_fatal()
REQUEST_URI/html/html-script.html