Examples of JavaScript Generating Output

0

All Contributions

Writing into an HTML element 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Problem with JavaScript Document.write() Method</title>
</head>
<body>
    <h1>This is a heading</h1>
    <p>This is a paragraph of text.</p>
    
    <button type="button" onclick="document.write('Hello World!')">Click Me</button>
</body>
</html>

Output :

Hello World!

30

Problem with document.write() method

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Problem with JavaScript Document.write() Method</title>
</head>
<body>
    <h1>This is a heading</h1>
    <p>This is a paragraph of text.</p>
    
    <button type="button" onclick="document.write('Hello World!')">Click Me</button>
</body>
</html>

This is a heading

This is a paragraph of text.

Writing into the browser window

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Writing into an Browser Window with JavaScript</title>
</head>
<body>
    <script>
    // Printing a simple text message
    document.write("Hello World!"); // Prints: Hello World!
    
    // Printing a variable value 
    var x = 10;
    var y = 20;
    var sum = x + y;
    document.write(sum); // Prints: 30
    </script>
</body>
</html>

Output :

Hello World!30

Writing into an alert dialog box 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Problem with JavaScript Document.write() Method</title>
</head>
<body>
    <h1>This is a heading</h1>
    <p>This is a paragraph of text.</p>
    
    <button type="button" onclick="document.write('Hello World!')">Click Me</button>
</body>
</html>

 

Writing into the browser console 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Problem with JavaScript Document.write() Method</title>
</head>
<body>
    <h1>This is a heading</h1>
    <p>This is a paragraph of text.</p>
    
    <button type="button" onclick="document.write('Hello World!')">Click Me</button>
</body>
</html>

Note: Please check out the browser console by pressing the f12 key on the keyboard.

total contributions (5)

This topic belongs to these lists

need a help?


find thousands of online teachers now

New to examplegeek.com?

Join us