Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Control html/js only

programming language of the Web

comment:
//single line

/*

Multiple line

*/

External file: myScript.js

function myFunction()

{ document.getElementById("demo").innerHTML = "Paragraph changed.";

<script src="myScript.js"></script>

Internal file:

<script>

document.getElementById("demo").innerHTML = "My First JavaScript";

</script>
Placing scripts in external files has some advantages:

Separate html and code

Makes html and js easier to read and maintain

Cached js files can speed up page loads

Change Paragraph
<!DOCTYPE html>

<html>

<head>

<script>

function myFunction() {

document.getElementById("demo").innerHTML = "Paragraph changed.";

</script>

</head>

<body>

<h2>Demo JavaScript in Head</h2>

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

<button type="button" onclick="myFunction()">Try it</button>

</body>

</html>
JavaScript can "display" data in different ways:

 Writing into an HTML element, using innerHTML.

 Writing into the HTML output using document.write().

 Writing into an alert box, using window.alert().

 Writing into the browser console, using console.log().

5+6

<!DOCTYPE html>

<html>

<body>

<h1>My First Web Page</h1>

<p>My First Paragraph</p>

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

<script>

document.getElementById("demo").innerHTML = 5 + 6;

</script>

</body>

</html>

<!DOCTYPE html>

<html>

<body>
<h1>My First Web Page</h1>

<p>My first paragraph.</p>

<script>

document.write(5 + 6);

</script>

</body>

</html>

<!DOCTYPE html>

<html>

<body>

<h1>My First Web Page</h1>

<p>My first paragraph.</p>

<button type="button" onclick="document.write(5 + 6)">Try it</button>

</body>

</html>
<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Statements</h2>

<p>Multiple statements on one line are allowed.</p>

<p id="demo1"></p>

<script>

let a, b, c;

a = 5; b = 6; c = a + b;

document.getElementById("demo1").innerHTML = c;

</script>

</button>

</body>

</html>
<!DOCTYPE html>

<html>

<body>

<h2>JavaScript switch</h2>

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

switch (new Date().getDay()) {

default:

text = "Looking forward to the Weekend";

break;

case 6:

text = "Today is Saturday";

break;

case 0:

text = "Today is Sunday";

document.getElementById("demo").innerHTML = text;

</script>

</body>

</html>
If default is not the last case in the switch block, remember to end the

default case with a break.

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript switch</h2>

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

switch (new Date().getDay()) {

case 4:

case 5:

text = "Soon it is Weekend";

break;

case 0:

case 6:

text = "It is Weekend";

break;

default:

text = "Looking forward to the Weekend";

document.getElementById("demo").innerHTML = text;

</script>

</body>
</html>

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript switch</h2>

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

let x = "0";

switch (x) {

case 0:

text = "Off";

break;

case 1:

text = "On";

break;

default:

text = "No value found";

document.getElementById("demo").innerHTML = text;

</script>

</body>

</html>

You might also like