JavaScript Programming/Introduction/Example Code

From Wikiversity
Jump to navigation Jump to search

example.html[edit | edit source]

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

  <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'">

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

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

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


example.js[edit | edit source]

//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!")