Server-side scripting is a script that runs on the web server, which sends results to your web browser. Some popular server-side scripting languages are PHP, Ruby, Python, and Perl.
- Directly in HTML page using a pair of <script> . . . </script> elements or tags
- As an event handler using HTML tag attribute.
- As a linked file with a .js extension via the src attribute of the <script> tag
Within <script> Element or Tag
This tag was created by Netscape and first implemented in Netscape Navigator 2. We can place any number of <script> tags within a web page. All <script> tags are interpreted in the order in which they are specified on the page.
Placing <script> tag in <head> Section
Program code 1:
<html> <head> <title>Script tags in the Head section </title> <script> document.write("Placing <script> tag between <head> section"); </script> </head> <body> </body> </html>
Placing <script> tag in <body> Section
On the other hand, if you want to execute script after loading full content on the web page, the script must be placed in between <body> …</body> portion.
Program code 2:
We can place an unlimited number of <script> tag in an HTML document. The below example illustrates a script in both head and body sections of HTML page.
Program code 3:
Attributes Added to <script> Element
There are six attributes available for adding within <script> element. Most of the modern browsers support these attributes. They are as follows:
The following examples show the use of async in the <script> tag.
<script src = "myScript1.js" async></script> <script src = "myScript2.js" async></script>
As you can see in the above examples, the use of async attribute within script tag is very simple.
2. charset: It is also optional. The character set of the code is specified using the src attribute within the script tag. This attribute is seldom used in the script tag because most browsers don’t honor its value.
3. defer: This attribute was introduced in HTML4. It tells the browser to defer the execution of script safely until after the document’s content has been completely parsed and displayed on the web page.
The defer is valid only for external scripts. Deferred scripts always run in the order in which they are written in the code.
The following examples demonstrate the use of defer attribute in the script tag.
<script src = "myScript.js" defer></script> Or, <script defer src = "myScript.js"></script>
6. type: This attribute tells the browser about the content type (also called MIME type) of the scripting language being used by the code block.
We define an event handler to trigger the execution of script in response to user activity such as a form action, key press, or mouse movement.
Some commonly used event handlers are onClick, onSubmit, onLoad, onMouseOut, and onMouseOver. All event handler attributes start with the word “on”.
Program code 4:
We will know more concepts with examples in Event handler chapter in the further tutorial.
Program code 5:
Output of code: Hello I am a pseudo-URL script
Here, Pseudo indicates that it is not actually a protocol like HTTPS, HTTP, or FTP but it works in the same way.