JavaScript Programming/Introduction/Example Code
Appearance
example.html
[edit | edit source]<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="Cache-Control"content="no-cache"><metahttp-equiv="Content-Security-Policy"content="script-src-elem 'unsafe-inline' 'self'"><title>Example</title><!-- The following line loads the external script. --><scriptdefersrc="example.js"async></script><script>//script in headdocument.write("Hello Wikiversity!");</script></head><body><noscript>Enable JavaScript to see web page content.</noscript><pid="text"></p><pid="html"></p><script>//script in bodywindow.alert("Hello Wikiversity!");</script></body></html>
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!")