JavaScript Where to (Cont.)


JavaScript in <head>
The example below calls a function, which is placed in the head section, when a button is clicked. The property hostName of the object location gives the host name.

     


Using an External JavaScript
JavaScript can also be placed in external files. External JavaScript files often contain code to be used on several different web pages. External JavaScript files have the file extension .js. Note that external script cannot contain the <script></script> tags! To use an external script, point to the .js file in the src attribute of the <script> tag:

           


In the JavaScript, the method prompt of the object window displays a prompt dialog box with a message and an input field. The concatenation operator + concatenates two string values together, returning another string that is the union of the two operand strings.



Demonstration
The following demonstration shows how the script of HTML and JavaScript is displayed on the Web.