JavaScript Programming/Introduction/Example Code

example.html edit

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="Cache-Control" content="no-cache">
  <meta http-equiv="Content-Security-Policy" content="script-src-elem 'unsafe-inline' 'self'">

  <title>Example</title>
  
  <!-- The following line loads the external script. -->
  <script defer src="example.js" async></script>

  <script>
    //script in head
    document.write("Hello Wikiversity!");
  </script>
</head>

<body>
  <noscript>Enable JavaScript to see web page content.</noscript>
  <p id="text"></p>
  <p id="html"></p>
  
  <script>
    //script in body
    window.alert("Hello Wikiversity!");
  </script>
</body>

</html>

example.js edit

//This script demonstrates JavaScript output five different ways.
//Two script blocks are in the HTML page. This script is external.

document.getElementById("text").innerText = "Hello Wikiversity!";
document.getElementById("html").innerHTML = "<strong>Hello Wikiversity!</strong>";
console.log("Hello Wikiversity!")