BLOG main image
분류 전체보기 (313)
교육 (13)
NEIS (6)
Edufine (0)
Programmer (5)
Android Programming (1)
Internet W3 (18)
JAVA Programming (9)
JSP/Servlet (1)
Framework (7)
Spring For Beginner (4)
eGovFrame (10)
MEAN Stack (2)
NodeJS (5)
SublimeText (30)
SublimeText_Tips (18)
Eclipse (16)
JavaScript (8)
jQuery (12)
jQuery_tips (1)
Ajax (3)
DWR(Direct Web Remote) (4)
JSON(JS Object Notation) (4)
Oracle (2)
MySQL (28)
OS (16)
Download (3)
Life (10)
Favorit Site (1)
Books (2)
Healthy (1)
Stocks (1)
HTML5/CSS (1)
Python (4)
Security (7)
CISSP (0)
Ruby On Rails (5)
일기장 (0)
영어 교과서(중2) (3)
알고리즘 (0)
Go Lang (3)
VB 2010 (12)
C# (1)
정보보안기사(네트워크보안) (0)
업무 활용 엑셀 (11)
틈틈이 활용팁 (14)
하루 하루 살아가며 ……. (2)
기술 (1)
파이썬 & 데이터분석 (1)
Visitors up to today!
Today hit, Yesterday hit
daisy rss
tistory 티스토리 가입하기!
2015. 7. 13. 17:04

JavaScript does NOT have any built-in print or display functions.


JavaScript Display Possibilities

JavaScript can "display" data in different ways:

  • Writing into an alert box, using window.alert().
  • Writing into the HTML output using document.write().
  • Writing into an HTML element, using innerHTML.
  • Writing into the browser console, using console.log().

Using window.alert()

You can use an alert box to display data:

 

<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>
<p>My first paragraph.</p>

<script>
    window.alert(5 + 6);
</script>

</body>
</html> 

Using document.write()

For testing purposes, it is convenient to use document.write():

 

<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>
<p>My first paragraph.</p>

<script>
  document.write(5 + 6);
</script>

</body>
</html> 

 

Using document.write() after an HTML document is fully loaded, will delete all existing HTML:

 

<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>
<p>My first paragraph.</p>

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

</body>
</html> 

The document.write() method should be used only for testing.

 

Using innerHTML

To access an HTML element, JavaScript can use the document.getElementById(id) method.

The id attribute defines the HTML element. The innerHTML property defines the HTML content:

 

<!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> 

 

To "display data" in HTML, (in most cases) you will set the value of an innerHTML property.

 

Using console.log()

In your browser, you can use the console.log() method to display data.

Activate the browser console with F12, and select "Console" in the menu.

 

<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>
<p>My first paragraph.</p>

<script>
  console.log(5 + 6);
</script>

</body>
</html> 

 

반응형

'JavaScript' 카테고리의 다른 글

[06] JavaScript Comments  (0) 2015.07.13
[05] JavaScript Statements  (0) 2015.07.13
[04] JavaScript Syntax  (0) 2015.07.13
[02] JavaScript Where To  (0) 2015.07.13
[01] JavaScript Intro  (0) 2015.07.13