How to Put a JavaScript Into an HTML Page

How to Put a JavaScript Into an HTML Page
<html> <body> <script type=”text/javascript”> document.write(“Hello World!”) </script> </body> </html>


The code above will produce this output on an HTML page:Hello World!Example ExplainedTo insert a script in an HTML page, we use the <script> tag. Use the type attribute to define the scripting language<script type=”text/javascript”>Then the JavaScript starts: The JavaScript command for writing some output to a page is document.writedocument.write(“Hello World!”)Then the <script> tag has to be closed</script>
Ending Statements With a Semicolon? With traditional programming languages, like C++ and Java, each code statement has to end with a semicolon.Many programmers continue this habit when writing JavaScript, but in general, semicolons are optional! However, semicolons are required if you want to put more than one statement on a single line.How to Handle Older BrowsersBrowsers that do not support scripts will display the script as page content. To prevent them from doing this, we may use the HTML comment tag:<script type=”text/javascript”> <!– some statements //–> </script>The two forward slashes at the end of comment line (//) are a JavaScript comment symbol. This prevents the JavaScript compiler from compiling the line.Note: You cannot put // in front of the first comment line (like //<!–), because older browsers will display it. Strange? Yes! But that’s the way it is.

Leave a Reply

Your email address will not be published.