HTML Scripts

HTML Scripts

How to add Script in HTML?

Javascript is used by web developers to make web pages interactive and to enhance their functionality. The <script> tag which should be included in the head section of a document enables you to include javascript code in your document. You can also write the javascript code on a separate document and then call it using the src attribute.

This method is efficient in situations where you intend to use the same script for several documents on your website. Just like attaching external CSS files, this method save you time that you would otherwise have used to repeat the same task over and over.

HTML5 Elements

Use the <nonscript> element to provide alternate content for users whose browsers do not support client-side scripting or those who have javascript disabled for security purposes. Note that the <nonscript> element can be used with all tags that are compatible with the <body> element.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Embedded JavaScript Example</title>        
</head>
<body>
    <script type="text/javascript">
        document.write("Hello World!");
    </script>
</body>
</html>

How to add an external script in HTML?

<head>
<script src="yourfile.js" type="text/javascript" />
</head>

How to add an internal script in HTML?

<head>
<title>Internal Script</title>
</head>
<body>
<script type="text/javascript">
   document.write("Hello Javascript!")
</script>
</body>

How to add event handler script in HTML?

<!DOCTYPE?html>?<html?lang="en">?
<head>
<title>Event Handler Example </title>
<script type="text/javascript">
function myAlert()
{
    alert("I am an event handler....");
    return;
}
</script>
</head>
<body>

<span onmouseover="myAlert();">
   Bring your mouse here to see an alert
</span>

</body>
</html>

How to Hide Scripts from Older Browsers?

JavaScript Example:
    <script type="text/javascript">
    <!--
    document.write("Hello Javascript!");
    //-->
    </script>

VBScript Example:
    <script type="text/vbscript">
    <!--
    document.write("Hello VBScript!")
    '-->
    </script>

How to add <noscript> tag in html?

JavaScript Example:
    <script type="text/javascript">
    <!--
    document.write("Hello Javascript!");
    //-->
    </script>
    <noscript>Your browser does not support Javascript!</noscript>
VBScript Example:
    <script type="text/vbscript">
    <!--
    document.write("Hello VBScript!")
    '-->
    </script>
    <noscript>Your browser does not support VBScript!</noscript>

How to add Default Scripting Language?

<meta http-equiv="Content-Script-Type" content="text/JavaScript" />

 

Terry White

Terry White is a professional Drupal & WordPress developer, Web developer, Web designer, Software Engineer, and Blogger. He strives for pixel-perfect design, clean robust code, and a user-friendly interface. If you have a project in mind and like his work, feel free to contact him

View all posts by Terry White →