JavaScript > Placing JavaScript code

Code in html page in JavaScript

How to write JavaScript code in the html page?


The JavaScript code can be kept into html page inside <script> tag.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Demo</title>

</head>
<body>
    
    <p style="color:red">Hello world using HTML!</p>

    <script>
        document.write("<b>Hello world using JavaScript!</b>");
    </script>

</body>
</html>

Above code snippet, shall write the text in inverted comma on the page in bold style. 

Note that <script> block ideally should be written inside the <head></head> block however it can be kept anywhere inside the <body></body> tag also.

 Views: 5688 | Post Order: 5



Write for us






Hosting Recommendations