-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
  • 331,213
  • 40
  • 305
  • 339
user4951
  • 32,206
  • 53
  • 172
  • 282
  • @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

5 Answers5

4

You could append a Text node to the body.

document.body.appendChild(document.createTextNode('Hello World!'));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
3

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
  • 331,213
  • 40
  • 305
  • 339
2

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,195
  • 1
  • 17
  • 34
  • 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
  • 278
  • 3
  • 17
-1
console.log("Hello World!")
Yog Sharma
  • 164
  • 2
  • 9
  • While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Shawn Hemelstrand Jan 26 '23 at 00:29