How to use script in HTML

In this article I will discuss the HTML Script.
  • 1922

A script is a small, embedded program that can add  to your website. For example, a script could generate a pop-up alert box message, or provide a dropdown menu. We use the<script> tag to generate the script using the scripting language. We can specify the language within the type attribute of the script tag, for example type may be JavaScript or vbscript.The most popular that we use is JavaScript as a scripting language as it support any browser whereas the vbscript only support the internet explorer.

Adding Script

We usually add the script within the head tag of html.This ensures that the script is ready to run when it is called. like,

<html>

<head>

    <title></title>

    <script type="text/javascript">

        document.write("Hello World!")

    </script>

</head>

<body>

</body>

</html>


This will print the Hello World! message in your browser. Lets take an another example,

 

<html>

<head>

    <title></title>

    <script type="text/javascript">

        alert("Hi! how are you?")

    </script>

</head>

<body>

</body>

</html>


This would open a JavaScript alert as soon as the page loads.

Calling an External Script
 

Sometimes we can make the scripting file and then use it in our application by the following line. In this suppose the JavaScript file name is  script.js and we use use it like, 
 

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

        alert("Hi! how are you?")

</script>


Hide script from the older browser

 

Although now a days almost all browser supports JavaScript but some are not, so we can hide the code. If a browser doesn't support JavaScript, instead of running your script, it would display the code to the user. To prevent this from happening, you can simply place HTML comments around the script. like,

 

<html>

<head>

    <title></title>

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

        <--hide from the old browser

        alert("Hi! how are you?")-->

    </script>

</head>

<body>

</body>

</html>

 

Alternate Information for old browser

 

Now if the browser does not support the script then we can provide the alternate information by using <noscript> tag. like,

 

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

        <--hide from the old browser

        alert("Hi! how are you?")-->

</script>
 

<noscript>

        You need JavaScript enabled to view this page.

</noscript>

 

Set a default Scripting Language

 

We can specify a default scripting language for all your script tags to use. This saves you from having to specify the language everytime you use a script tag within the page. like,

 

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

 

Ask Your Question 
 
Got a programming related question? You may want to post your question here
 
Programming Answers here

 

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.