-2

I've been looking all over the world.

All I see is samples alerting hello world

I don't want to alert hello world.

I want to print a simple website saying hello world.

<!DOCTYPE html>
<html lang="en">
<head></head>
<body id="home">
    <script>
        //print hello world
    </script>
</body>
</html>

Does javascript have a print command?

Here are typical samples on the web

http://groups.engin.umd.umich.edu/CIS/course.des/cis400/javascript/hellow.html

Rory McCrossan
  • 319,460
  • 37
  • 290
  • 318
user4951
  • 31,039
  • 51
  • 162
  • 275
  • @RoryMcCrossan I thought that as well, but it doesn't make the *website* print hello world ;). – roberrrt-s Jan 05 '17 at 10:19
  • 1
    JS: `document.getElementById('home').textContent = 'hello world';` jQ: `$(function() { $('#home').text('hello world'); });` – Rory McCrossan Jan 05 '17 at 10:19
  • `console.log` is that you want to get accustomed to for testing purposes. There is no 'printing' on the page as the page itself consists of elements which you need to refer to. – Dellirium Jan 05 '17 at 10:20
  • http://javascript.info/tutorial/hello-world – Nagaraju Jan 05 '17 at 10:22
  • You'll need to define what you mean by "print" - for most people "print" means, "send to a printer", which doesn't seem likely. Every language uses a different term for what(whatever) you are trying to do. – freedomn-m Jan 05 '17 at 10:39

4 Answers4

3

You could append a Text node to the body.

document.body.appendChild(document.createTextNode('Hello World!'));
Nina Scholz
  • 351,820
  • 24
  • 303
  • 358
2

In JS code you don't 'print' to the screen. Instead you amend the properties of the HTML elements in the DOM.

To do what you require you can retrieve the #home element then set its text. Either of the below will work for you:

// POJS
document.getElementById('home').textContent = 'hello world';

// jQuery
$(function() {  
    $('#home').text('hello world'); 
});

<!DOCTYPE html>
<html lang="en">
<head></head>
<body id="home">
    <script>
        document.getElementById('home').textContent = 'hello world';
    </script>
</body>
</html>
Rory McCrossan
  • 319,460
  • 37
  • 290
  • 318
1

In JavaScript Write some text directly to the HTML document Use document.write();.

like below.

<script>
document.write("Hello World!");
</script> 

<!DOCTYPE html>
<html>
<body>

<script>
document.write("Hello World!");
</script>

</body>
</html>
Pravin Vavadiya
  • 3,099
  • 1
  • 15
  • 33
  • 2
    While this may work, using `document.write` is considered extremely bad practice. I really wouldn't recommend it - especially to a beginner. – Rory McCrossan Jan 05 '17 at 10:22
  • 1
    Its terrible and should pretty much never be used but I guess this is still the correct answer to the question asked! – Lucero Jan 05 '17 at 10:26
0

You can use:

document.write("hello world");

but I will not recommend it (for more information look here).

You can use this instead:

document.getElementById("home").textContent = "hello world";
Mehan Alavi
  • 49
  • 1
  • 7