JavaScript How To

The HTML <script> tag is used to insert a JavaScript into an HTML page.


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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值