As mentioned earlier, Javascript is used to make the site dynamic. So, we can dynamically change the contents of the webpage. So, lets begin javascript with a hello world program.

<html>
<body>
<script type='text/JavaScript'>
document.write('Hello World!')
</script>
</body>
</html>

The above program prints a Hello World message on the screen. Please note that we have used a written the javascript code directly with HTML because its just one line of code.

document.write has been used to write a text to our page. However, we can also write some HTML script using it. However, it is not advisable to write contents to webpage using document.write() because it deletes every other thing on page.

So, we write to particular part of the document using DOM. We directly pick up the div whose contents need to be changed and change it according to our wish. Here is an example :-


<html>
<body>

<h1>My First Jscript code for writing text</h1>

<p id='trial'></p>

<script type='text/javascript'>
document.getElementById('trial').innerHTML='Hello World !';
</script>

</body>
</html>

The method getElementById selects the Id searched for and return the selected object

Some of the older browsers do not support javascript, or , if the javascript is set to OFF by the user, the browser is not able to interpret the javascript code. So, we usually put the entire code in comments, so that those browsers can neglect those parts. Otherwise, they would simply print the javascript code. Many a times , we also use a noscript tag. The <noscript> tag is used to provide an alternate content for users that have disabled scripts in their browser or have a browser that doesn't support client-side scripting

<html>
<body>
<p id='trial'></p>
<script type='text/javascript'>
<!--
document.getElementById('trial').innerHTML='Hello World !';
//-->
</script>
</body>
</html>

// example of noscript tag

<noscript>Your browser does not support JavaScript!</noscript>

Lets proceed to some more javascript code now.

Member Login

Member Login




Not a Member? Sign Up!




Login to comment

Be the first to comment on this topic







  


<<< Wanna review

Continue >>>