Skip to main content

Writing to The HTML Document

Writing to The HTML Document

The example below writes a <p> element with current date information to the HTML document:

Example

<html>
<body>

<h1>My First Web Page</h1>

<script type="text/javascript">
document.write("<p>" + Date() + "</p>");
</script>


</body>
</html>

Try it yourself »
Note: Try to avoid using document.write() in real life JavaScript code. The entire HTML page will be overwritten if document.write() is used inside a function, or after the page is loaded. However, document.write() is an easy way to demonstrate JavaScript output in a tutorial.

Changing HTML Elements

The example below writes the current date into an existing <p> element:

Example

<html>
<body>

<h1>My First Web Page</h1>

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

<script type="text/javascript">
document.getElementById("demo").innerHTML=Date();
</script>


</body>
</html>

Try it yourself »
Note: To manipulate HTML elements JavaScript uses the DOM method getElementById(). This method accesses the element with the specified id.

Examples Explained

To insert a JavaScript into an HTML page, use the <script> tag.
Inside the <script> tag use the type attribute to define the scripting language.
The <script> and </script> tells where the JavaScript starts and ends:
<html>
<body>
<h1>My First Web Page</h1>

<p id="demo">This is a paragraph.</p>

<script type="text/javascript">
... some JavaScript code ...
</script>

</body>
</html>
The lines between the <script> and </script> contain the JavaScript and are executed by the browser.
In this case the browser will replace the content of the HTML element with id="demo", with the current date:
<html>
<body>
<h1>My First Web Page</h1>

<p id="demo">This is a paragraph.</p>

<script type="text/javascript">
document.getElementById("demo").innerHTML=Date();
</script>

</body>
</html>
Without the <script> tag(s), the browser will treat "document.getElementById("demo").innerHTML=Date();" as pure text and just write it to the page: Try it yourself

Some Browsers do Not Support JavaScript

Browsers that do not support JavaScript, will display JavaScript as page content.
To prevent them from doing this, and as a part of the JavaScript standard, the HTML comment tag should be used to "hide" the JavaScript.
Just add an HTML comment tag <!-- before the first JavaScript statement, and a --> (end of comment) after the last JavaScript statement, like this:
<html>
<body>
<script type="text/javascript">
<!--
document.getElementById("demo").innerHTML=Date();
//-->
</script>
</body>
</html>
The two forward slashes at the end of comment line (//) is the JavaScript comment symbol. This prevents JavaScript from executing the --> tag.

Comments

Popular posts from this blog

Invigilators caught helping relatives during exams

HYDERABAD: The Chairman Board of Intermediate and Secondary Education (BISE) Hyderabad Abdul Aleem Khanzada, on Monday, detected four invigilators helping their sisters and daughters in cheating during Higher Secondary School Certificate Part-I and Part-II Annual Examinations 2012. According to a spokesman, the Chairman BISE Hyderabad caught these invigilators red handed during his surprise visit to examination centres of Tando Allahyar and Chambar along with his team members. While taking spot action, the Chairman BISE Hyderabad has relieved these four invigilators from the duty of conducting examinations. Besides, he also caught red handed 21 male and female candidates who were engaged in copying in the examinations. The other vigilance committees of the board which conducted surprise visits to Hyderabad, Shaheed Benazirabad, Matiari, Tando Allahyar, Tando Muhammad Khan, Badin, Thatta, Jamshoro and Dadu districts detected 144 male and female candidates engaged in copying during the

Ufone TV Commercials Free To View or Download

30 Second Offer Sim Lagao Offer Ufone Winner Offer