Javascript Examples
In JavaScript document.write() function is used to display dynamic content.
Let’s create the JavaScript Example:
Write JavaScript code into Html Page.
Write JavaScript code in external file.
1.) Write JavaScript code into Html Page
Now we will see how a JavaScript code is write into Html page
➤ Example 1: index.html
<html> <head> </head> <body> <script> document.write("Hello JavaScript!"); </script> </body> </html>
Output :
Let's see, the code above will produce this output on an Html Page
Example Explained :
To insert a JavaScript into an HTML page, We use the <script> tag (You also use the type attributes to define the scripting language).
The word document.write is a standard JavaScript command for writing output to the page. By entering the document.write command between the <script type="text/javascript"> and </script> tags, the browser will recognize it as a JavaScript command and execute the code line. In this case browser will write "Hello JavaScript!" to the page.
2.) Write JavaScript code in external file.
Let's see, how to Write JavaScript code in external file.
➤ External file: myScriptFile.js
document.write("I am external JavaScript file!");
Now we will use external JavaScript file :
In this example links to a <script> located in the same folder as the current page:
➤ Example 2 : index2.html
<html> <head> <script src="myScriptFile.js"> </script> </head> <body> </body> </html>
Comments