How Do You Get JavaScript to Present in a Web Browser?

Problem scenario
You have a .js file on your desktop or on a web server. You want to have the code execute and present itself nicely (not just see the raw code) in a web browser. How do you do this?

Solution
You must use the .html extension. You must have

<html> </html> 

tags. Here is an example. Name this file contint.html and place it on your desktop, and then open it with a web browser.

<!DOCTYPE html>
<html>
<body>

<h2>Use JavaScript to Change Text</h2>
<p>This example writes "Hello in JavaScript from Continualintegration.com!" into an HTML element with id="demo":</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = "Hello in JavaScript from Continualintegration.com!";
</script> 

</body>
</html>

Leave a comment

Your email address will not be published. Required fields are marked *