Script tag and Html Attributes:

The <script> Tag :

The script tag has two purposes:
1. It identifies a block of script in the page.
2. It loads a script file.
What it does depends on the presence of src attribute.
src=”url”
The src attribute is optional. If it is present, then its value is a url which identifies a .js file. The loading and processing of the page pauses while the browser fetches, compiles, and executes the file. The content between the <script src=”url”> and the </script> should be blank.
If the src attribute is not present, then the content text between the <script> and the </script> is compiled and executed. In older versions of HTML it was necessary to specify a value for “type”. The scripting language is specified by the “type” attribute.
<script type=”text/javascript”>
// JavaScript code here
</script>
However in HTML5, “type” defaults to “text/javascript”. Therefore, HTML5 <script> tags can be shortened in the following fashion.
<script>
// JavaScript code here
</script>
By default script tag is for client side if we want it to run on server side we have to add attribute which tells it runs on server side.
The document.write method provides a way of incorporating strings into the HTML content of the page.This simply prints the specified text to the page.
<script>
document.write(‘*Hello, there!*’)
</script>

Changing HTML Content:

<!DOCTYPE html>
<html>
<body>
<h1>My First JavaScript</h1>
<p id=”demo”>
JavaScript can change the content of an HTML element.
</p>
<script>
function myFunction()
{
x=document.getElementById(“demo”); // Find the element
x.innerHTML=”Hello JavaScript!”; // Change the content
}
</script>
<button type=”button” onclick=”myFunction()”>Click Me!</button>
</body>
</html>
In this example we have assigned id to paragraph element which is demo. In the script tag we can also define funtions by using function keyword which is followed by the name of funtion myFunction in this case. The getElementById funtion returns html element having demo as id so in this case it returns paragraph element. The innerHTML funtion is used to change content of paragraph element.
Button tag <button> is used for buttons. Its onclick event is assigned funtion myFunction so whenever the button is clicked it calls the funtion.

Html Attributes:

The rel attribute is used to provide information about the relationship between the document that’s being linked to (as defined in the href attribute) and the referencing document.One of the most common uses of the rel attribute, in the context of the a element, is to specify a value of “nofollow”. This is to indicate to search engines that the linked document should not be indexed by the search engine, and that any reputation/value associated with the linking site is not passed on such that it benefits the linked page in search results.
The href attribute defines the document to which the link leads. This may be a web page in the same directory, a page somewhere else on the same server, a location within the current page, or a web page—or any another kind of document—stored on another server.

Comments are closed.