Category: Header

  • JavaScript

    JavaScript allows you to create dynamic and interactive HTML pages. JavaScript is a high-level programming language and core technology behind web developments.

    script is a small piece of program that can add interactivity to our websites. For example, a script could generate a pop-up alert box message or provide a dropdown menu based on certain conditions, such as a user clicking a button. This script could be written using JavaScript or VBScript. Nowadays, only JavaScript and associated frameworks are being used by most web developers; VBScript is not even supported by major browsers.

    Adding JavaScript in HTML

    There are multiple ways of adding scripts (JavaScript) to the HTML document. We can keep JavaScript code in a separate file and then include it wherever it’s needed, or it is also possible to define functionality inside the HTML document itself.

    You can add JavaScript in an HTML document by using the <script> tag. The following are the different ways to add JavaScript into an HTML page:

    • Inline JavaScript
    • Internal JavaScript
    • External JavaScript

    Let’s see both cases one by one with suitable examples.

    Writing Inline JavaScript

    You can directly add JavaScript code to an HTML element on the various attributes, such as onclickonload, or any other.

    Example

    In the below example, we are writing a script to print “Hi there!” on button click:

    <!DOCTYPE html><html><head><title>Example of Inline JavaScript</title></head><body><button onclick="alert('Hi there!')">Click Me</button></body></html>

    Writing Internal JavaScript

    You can write the JavaScript code directly into our HTML document. Usually, we keep script code in the header of the document inside the <script> tag; however, there is no restriction. You are allowed to put the script code anywhere in the document but inside the <script> tag.

    Example

    In the following example, we are writing internal JavaScript code in the HTML page; it will display “Hello, World” on button click:

    <!DOCTYPE html><html><head><title>JavaScript Internal Script</title><base href="http://www.tutorialspoint.com/" /><script type="text/JavaScript"> function Hello(){
    
      alert("Hello, World");
    } </script></head><body><input type="button" onclick="Hello();" name="ok" value="Click Me" /></body></html>

    Adding External JavaScript

    If developers are going to define a functionality that will be used in various HTML documents, then it’s better to keep that functionality in a separate JavaScript file and then include that file in the HTML documents. A JavaScript file will have an extension of .js.

    An external JavaScript file is imported (added) using the <script> tag by providing the path to the file in the src attribute. You should also define the type attribute with the “text/javascript” value.

    Example

    Consider we define a small function using JavaScript in script.js, which has the following code −

    function Hello() {
       alert("Hello, World");
    }
    

    Now, let’s make use of the above external JavaScript file in our following HTML document −

    <!DOCTYPE html><html><head><title>JavaScript External Script</title><script src="/html/script.js" type="text/JavaScript" /></script></head><body><input type="button" onclick="Hello();" name="ok" value="Click Me" /></body>undefined
    </html>

    Event Handlers

    Event handlers aare nothing but simply defined functions that can be called against any mouse or keyboard event. We can define any kind of business logic inside our event handler, which can vary from a single to thousands of lines of code.

    Example

    The Following example explains how to write an event handler. We are going to write one simple function named EventHandler() in the header of the document. We will call this function when any user hovers the mouse over a paragraph:

    <!DOCTYPE html><html><head><title>Event Handlers Example</title><base href="http://www.tutorialspoint.com/" /><script type="text/JavaScript"> function EventHandler(){
    
      alert("I'm event handler!!");
    } </script></head><body><p onmouseover="EventHandler();">Bring your mouse here to see an alert</p></body></html>

    After running the above HTML code, bring your mouse over the output screen to see the result.

    Hide Scripts from Older Browsers

    Although most browsers these days support JavaScript, some older browsers don’t. If a browser doesn’t support JavaScript, instead of running your script, it would display the code to the user. To prevent this, we can simply place HTML comments around the script as shown below.

    Example

    In the following example, we are demonstrating how you can hide scripts (JavaScript and VbScript) from older browsers:

    JavaScript Example:

    <script type="text/JavaScript"><!--
       document.write("Hello JavaScript!");//--></script>

    VBScript Example:

    <script type="text/vbscript"><!--
       document.write("Hello VBScript!")
       '--></script>

    The <noscript> Element

    For users whose browsers do not support scripts, or for those who have disabled scripts in their browsers, we can embed scripts into the web page using the <noscript> tag as illustrated in the below example.

    Example

    In the following example, we are demonstrating how you can use the <noscript> tag for JavaScript and VbScript:

    JavaScript Example:

    <script type="text/JavaScript"><!--
       document.write("Hello JavaScript!");
       //--></script><noscript>Your browser does not support JavaScript!</noscript>

    VBScript Example:

    <script type="text/vbscript"><!--
       document.write("Hello VBScript!")
       '--></script><noscript>Your browser does not support VBScript!</noscript>

    Default Scripting Language

    There may be a situation when we are required to include multiple script files and ultimately use multiple <script> tags. We can specify a default scripting language for all the script tags at once. This saves us from specifying the language every time we use a script tag within the page.

    Example

    The following example demonstrates using the default scripting language in an HTML page:

    <meta http-equiv="Content-Script-Type" content="text/JavaScript" />

    Note that you can still override the default by specifying a language within the <script> tag.

    HTML Script Tags

    The following two are the script-related tags used in the HTML document:

    Script TagUse
    <script>It is used to import or write JavaScript inside an HTML document.
    <noscript>If a browser does not support script, it is used to define alternate content for users.
  • Adding  Favicon

    HTML favicon stands for “favorite icon”. It is a small-sized image that displays in the browser’s tab just before the page title. Favicon is defined by using the <link> tag with the “rel=icon” attribute.

    What is a HTML Favicon?

    favicon is a small image that represents your website and helps users identify it among multiple tabs, bookmarks, and search results. It can be in various formats, such as ICO, PNG, GIF, JPEG, or SVG, but ICO is the most widely supported format. If you have ever visited a website and noticed a small icon next to the page title in your browser’s tab, you have seen a favicon.

    Visual Representation of a Favicon

    The below image demonstrates how a favicon looks like on the browser’s tab:

    Visual Representation Favicon

    In the above figure, we can see the favicon of the tutorialspoint official website appear on the top of each tab.

    How To Add a Favicon in HTML

    You can add a favicon to a webpage by using the <link> tag with the rel attribute set to "icon". The <link> tag is a head element, so it must be placed within the <head> tag.

    Syntax

    You can add a favicon on the webpage as follows:

    <head><link rel="icon" href="path_to_favicon.ico" type="image/x-icon"></head>

    Steps to Add Favicon on Webpage

    To add a favicon, we need to follow these simple steps mentioned below −

    Step 1 − Create or choose an image for your favicon. Its common size could be 16×16 pixels or 32×32 pixels. There are a few online tools available that help us in creating a favicon, such as “Favicon.io” and “ionos.com”.

    Step 2 − Save and upload the favicon image to the website directory. Make sure the image is in a format that browsers can recognize, such as PNG, GIF, or ICO.

    Step 3 − Now use the <link> element, which tells the browser where to find the favicon image. Remember, the <link> tag comes inside the header part, i.e., <head> tag of the HTML document.

    Example

    The following example illustrates how to create an HTML favicon. We are using the PNG image format:

    <!DOCTYPE html><html><head><title>Tutorialspoint</title><link rel = "icon" type = "image/png" href = "images/faviconTP.png"></head><body><h1>Adding Favivon</h1><p>This is an example of including favicon to the web page.</p><p> Favicon will be displayed in the browser tab to the left of the page title.</p></body></html>

    Output

    The above HTML code will produce the following result −

    Adding Favicon

    Different Favicons for Different Pages on a Website?

    Yes, different favicons can be added for different pages on a website. You need to define the favicon images for different pages using the <link> tag (as discussed above) inside the head.

    Example

    Consider the following code snippets for the different pages:

    For Webpage 1:

    <head><title>Page Title 1</title><link rel="icon" href="favicon1.ico" type="image/x-icon"></head>

    For Webpage 2:

    <head><title>Page Title 2</title><link rel="icon" href="favicon2.ico" type="image/x-icon"></head>

    Favicon Dimensions and Sizes

    The following is a list of the various sizes for favicons:

    Favicon DimensionUsed For
    32×32Desktop Browsers
    57×57Mac ios
    76×76Apple ipad
    96×96Google TV
    120×120Iphone Retina Touch Screen
    128×128Chrome Web Store, Windows 8* Screen
    144×144Internet Explorer 10 Metro
    152×152Apple Ipad
    167×167Apple Ipad
    180×180Apple Iphones
    192×192Google Developer Apps
    195×195Opera Speed Dial
    196×196Android Home of Chrome
    228×228Opera Cast Icon

    Browser Support for Different Favicon File Format

    The table below illustrates the various favicon file formats that are supported by different browsers −

    BrowserGIFPNGJPEGSVGICO
    ChromeYesYesYesYesYes
    EdgeYesYesYesYesYes
    SafariYesYesYesYesYes
    FirefoxYesYesYesYesYes
    OperaYesYesYesYesYes

  • Head Elements

    HTML head elements define metadata like the title, character set, links to external stylesheets, and other details. This information does not display on the webpage but is helpful for the search engines and browsers. The head elements are placed inside the <head> tag.

    The following are the commonly used head elements:

    • <title> Element
    • <meta> Element
    • <base> Element
    • <link> Element
    • <style> Element
    • <script> Element

    Let’s understand each element in detail with the help of examples.

    HTML <title> Element

    The HTML <title> tag is used for specifying the title of the HTML document. The title must describe the content of the web page, and its format should be text only. It appears in the title bar of the browser’s tab.

    Example

    Following is an example that shows how to give a title to an HTML document using the <title> tag:

    <!DOCTYPE html><html><head><title>HTML Title Tag Example</title></head><body><p>Describing the use of title tag</p></body></html>

    HTML <meta> Element

    The HTML <meta> tag is used to provide metadata about an HTML document. The metadata is nothing but additional information about the web page, including page expiry, page author, list of keywords, page description, and so forth. This information is further used for the purpose of search engine optimization. Remember, the metadata specified by the <meta> tag is not displayed on the web page, but it is machine-readable. Its most commonly used attributes are namecontentcharset, and http-equiv.

    Read More: HTML Meta Tags

    Example

    The following example describes a few of the important usages of the <meta> tag inside an HTML document:

    <!DOCTYPE html><html><head><title>HTML Meta Tag Example</title><!-- Provide list of keywords --><meta name="keywords" content="C, C++, Java, PHP, Perl, Python"><!-- Provide description of the page --><meta name="description" content="Simply Easy Learning by Tutorials Point"><!-- Author information --><meta name="author" content="Tutorials Point"><!-- Page content type --><meta http-equiv="content-type" content="text/html; charset=UTF-8"><!-- Page refreshing delay --><meta http-equiv="refresh" content="30"><!-- Page expiry --><meta http-equiv="expires" content="Wed, 21 June 2006 14:25:27 GMT"><!-- Tag to tell robots not to index the content of a page --><meta name="robots" content="noindex, nofollow"></head><body><p>Describing the use of HTML meta tag</p></body></html>

    HTML <base> Element

    The HTML <base> tag is used for specifying the base URL for all relative URLs in a page, which means all the other URLs will be concatenated into the base URL while locating the given item. We are allowed to use only one base element in our HTML document. The most frequently used attributes of the tag are hrefandtarget.

    Example

    In this example, all the given pages and images will be searched after prefixing the given URLs with the base URL http://www.tutorialspoint.com/ directory:

    <!DOCTYPE html><html><head><title>HTML Base Tag Example</title><base href = "index.htm/" /></head><body><img src="/images/logo.png" alt="Logo Image"/><a href="/html/index.htm" title="HTML Tutorial"/>HTML Tutorial</a></body></html>

    On running the above code, the tutorialspoint logo along with a link will be displayed. If we click on that link, it will redirect us to the index page of the HTML tutorial.

    In HTML, the <link> tag is used to specify relationships between the current webpage and another external resource. The source of external resources is placed inside thehref attribute. The other attributes of the tag are reltype, and media. Its most common use is to embed stylesheets into the HTML document.

    Example

    Following is an example of linking an external style sheet file available in the “css” subdirectory within the web root:

    <!DOCTYPE html><html><head><title>HTML link Tag Example</title><link rel="stylesheet" type="text/css" href="/css/style.css"></head><body><p>It is an example of linking stysheet to the current HTML document.</p></body></html>

    HTML <style> Element

    The HTML <style> tag is used to specify styles either for the whole HTML document or for a particular element. Its most common attributes are title and media.

    Example

    In the following example, we are defining a style for the <p> tag using the <style> tag.

    <!DOCTYPE html><html><head><title>HTML style Tag Example</title><base href="http://www.tutorialspoint.com/" /><style>
    
      .myclass{
        background-color: #aaa;
        padding: 10px;
      }
    </style></head><body><p class="myclass">Hello, World!</p></body></html>

    Note − To learn about how Cascading Style Sheets work, kindly check a separate tutorial available at CSS Tutorial.

    HTML <script> Element

    The HTML <script> tag is used to include either an external script file or to define an internal script for the HTML document. The script is an executable code that performs some action.

    Example

    Following is an example where we are using a script tag to define a simple JavaScript function. When the user clicks on the OK button, an alert box will pop up with a Hello, Worldmessage.

    <!DOCTYPE html><html><head><title>HTML script Tag Example</title><base href="http://www.tutorialspoint.com/" /><script type="text/JavaScript">
    
      function Hello(){
         alert("Hello, World");
      }
    </script></head><body><input type="button" onclick="Hello();" name="ok" value="OK" /></body></html>