

{"id":78932,"date":"2020-07-04T09:00:34","date_gmt":"2020-07-04T03:30:34","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=78932"},"modified":"2024-07-24T17:03:56","modified_gmt":"2024-07-24T11:33:56","slug":"html-javascript","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/html-javascript\/","title":{"rendered":"HTML JavaScript &#8211; Add Javascript File to HTML"},"content":{"rendered":"<p>HTML documents can be made interactive and dynamic using a script. A script is a piece of code that adds dynamism to a website. The popular scripting languages are VBScript, JavaScript, Python, etc. Nowadays, JavaScript, along with its frameworks, are prevailing and used for web development. The JavaScript file can be stored with \u2018.js\u2019 extension and can be accessed using the &lt;script&gt; tag of HTML.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/HTML-JavaScript-df.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78979\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/HTML-JavaScript-df.jpg\" alt=\"HTML JavaScript\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/HTML-JavaScript-df.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/HTML-JavaScript-df-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/HTML-JavaScript-df-1024x536.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/HTML-JavaScript-df-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/HTML-JavaScript-df-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/HTML-JavaScript-df-520x272.jpg 520w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><\/p>\n<h2>Advantages of JavaScript in HTML<\/h2>\n<p>1. Client-side Language &#8211; JavaScript is a client-side language, i.e. it is executed on the processor of the user, thus reducing the load on the web-server and bandwidth.<\/p>\n<p>2. Easy to Learn &#8211; We can easily learn JavaScript because of its syntax closer to the English language and a large number of predefined functionalities.<\/p>\n<p>3. Faster Execution &#8211; JavaScript provides faster execution since it executes on the client-side and does not consume any server bandwidth.<\/p>\n<p>4. Compiler-free &#8211; JavaScript does not need any explicit compiler, as it is interpreted as the HTML tags.<\/p>\n<p>5. Test and Debug &#8211; It is easy to test and debug the JavaScript code.<\/p>\n<p>6. Platform &#8211; JavaScript is a platform-independent scripting language.<\/p>\n<p>7. Event-based &#8211; JavaScript provides event-based programming, i.e.;certain methods are executed when a particular event occurs.<\/p>\n<p>8. Procedural Programming &#8211; Procedural programming capabilities such as condition-checking, looping, branching, etc. can be easily executed using JavaScript.<\/p>\n<p>9<strong>. <\/strong>Prevailing Frameworks &#8211; JavaScript has become extremely popular with the advent of Node.js, being used as the back-end.<\/p>\n<p>Moreover, JavaScript supports functional programming styles, making it versatile for different programming paradigms. This flexibility makes development easier while at the same time enabling the developers to write good and maintainable code. Additionally, with the integration of modern ES6+ features like arrow functions, promises, and async\/await, JavaScript has become more powerful and efficient, enhancing the overall developer experience.<\/p>\n<p>With the help of JavaScript, numerous libraries and frameworks have been developed, which are React, Angular, and Vue. js. These tools help reduce the development time and make it quite easy to develop complex and high performing applications. Due to this extensive support and community backing, JavaScript cannot be overlooked as a key requisite for current web development.<\/p>\n<h2>HTML &lt;script&gt; tag<\/h2>\n<p>The script tag of an HTML document contains the client-side script, used to perform tasks on that particular document. It contains either the code written implicitly or an external file, specified in the \u2018src\u2019 attribute with an extension \u2018.js\u2019.<\/p>\n<h3>External JavaScript<\/h3>\n<p><strong>Code-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;title&gt;External Script&lt;\/title&gt;\r\n&lt;script src = \"script.js\" type = \"text\/javascript\"\/&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;input type = \"button\" onclick = \"myFunction();\" name = \"okay\" value = \"Click\"&gt; &lt;\/input&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p><strong>Script.js &#8211;<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">function myFunction() {\r\n  alert(\"Hello, World\");\r\n}<\/pre>\n<p><strong>Output-<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-1external-javascript.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78981\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-1external-javascript.png\" alt=\"external javascript\" width=\"1262\" height=\"209\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-1external-javascript.png 1262w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-1external-javascript-300x50.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-1external-javascript-1024x170.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-1external-javascript-150x25.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-1external-javascript-768x127.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-1external-javascript-520x86.png 520w\" sizes=\"auto, (max-width: 1262px) 100vw, 1262px\" \/><\/a><\/p>\n<h3>Internal JavaScript<\/h3>\n<p><strong>Code-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;title&gt;External Script&lt;\/title&gt;\r\n&lt;script&gt;\r\n  function myFunction() {\r\n   alert(\"Hello, World\");\r\n}\r\n&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;input type = \"button\" onclick = \"myFunction();\" name = \"okay\" value = \"Click\"&gt; &lt;\/input&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p><strong>Output-<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-2internal-javascript.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78982\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-2internal-javascript.png\" alt=\"internal javascript\" width=\"1248\" height=\"208\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-2internal-javascript.png 1248w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-2internal-javascript-300x50.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-2internal-javascript-1024x171.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-2internal-javascript-150x25.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-2internal-javascript-768x128.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-2internal-javascript-520x87.png 520w\" sizes=\"auto, (max-width: 1248px) 100vw, 1248px\" \/><\/a><\/p>\n<p>The script tag can be placed anywhere within the document, but usually, it is kept in the &lt;head&gt; tag.<\/p>\n<h2>HTML JavaScript Event Handlers<\/h2>\n<p>As seen from the name, event handlers are functions called after occurrence of a particular event. This event may be a button on-click, mouse hovering, keyboard-related event, etc.<\/p>\n<p><strong>Code-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n   &lt;head&gt;\r\n      &lt;script type = \"text\/JavaScript\"&gt;\r\n         function EventHandler() {\r\n            alert(\"Welcome to DataFlair\");\r\n         }\r\n      &lt;\/script&gt;\r\n   &lt;\/head&gt;\r\n   &lt;body&gt;\r\n   &lt;p onmouseover = \"EventHandler();\"&gt;Roll the mouse here&lt;\/p&gt;\r\n   &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p><strong>Output-<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-3event-handlers.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78983\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-3event-handlers.png\" alt=\"html event handlers\" width=\"1230\" height=\"193\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-3event-handlers.png 1230w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-3event-handlers-300x47.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-3event-handlers-1024x161.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-3event-handlers-150x24.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-3event-handlers-768x121.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-3event-handlers-520x82.png 520w\" sizes=\"auto, (max-width: 1230px) 100vw, 1230px\" \/><\/a><\/p>\n<h2>HTML Events with JavaScript<\/h2>\n<p><a href=\"https:\/\/itconnect.uw.edu\/learn\/workshops\/online-tutorials\/web-publishing\/javascript\/\">JavaScript<\/a> is for handling certain events. Its syntax is &lt;element event=\u201dJavaScript\u201d&gt;. The events can be of the following types-<\/p>\n<h3>Common HTML Events<\/h3>\n<table>\n<tbody>\n<tr>\n<td><b>Event<\/b><\/td>\n<td><b>Description<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">onchange<\/span><\/td>\n<td><span style=\"font-weight: 400\">Occurs when there is a change in an HTML element.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">onclick<\/span><\/td>\n<td><span style=\"font-weight: 400\">Occurs when the user clicks an HTML element.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">onmouseover<\/span><\/td>\n<td><span style=\"font-weight: 400\">This occurs when the mouse moves over an HTML element.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">onmouseout<\/span><\/td>\n<td><span style=\"font-weight: 400\">Occurs when the mouse moves away from the HTML element.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">onkeydown<\/span><\/td>\n<td><span style=\"font-weight: 400\">Occurs when we press a key.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">onload<\/span><\/td>\n<td><span style=\"font-weight: 400\">It occurs when the page is loaded by the browser.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>getElementByID() method (Change HTML content)-<\/strong><\/p>\n<p>In order to select an element in HTML document and perform dynamic tasks on it, the getElementByID() method is useful.<br \/>\n<strong>Code-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;body&gt;\r\n&lt;h1&gt;JavaScript&lt;\/h1&gt;\r\n&lt;p&gt;JavaScript to change the content of an HTML element:&lt;\/p&gt;\r\n&lt;button type=\"button\" onclick=\"myFunction()\"&gt;Click&lt;\/button&gt;\r\n&lt;p id=\"demo\"&gt;Initially&lt;\/p&gt;\r\n&lt;script&gt;\r\nfunction myFunction() { \r\n  document.getElementById(\"demo\").innerHTML = \"Hello World!\";\r\n}\r\n&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n\r\n<\/pre>\n<p><strong>Output-<\/strong><br \/>\nBefore-<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-4before-getElementByID.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78984\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-4before-getElementByID.png\" alt=\" getElementByID()\" width=\"459\" height=\"204\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-4before-getElementByID.png 459w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-4before-getElementByID-300x133.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-4before-getElementByID-150x67.png 150w\" sizes=\"auto, (max-width: 459px) 100vw, 459px\" \/><\/a><\/p>\n<p>After-<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/image-5after-getElementByID.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78985\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/image-5after-getElementByID.png\" alt=\"getElementByID()\" width=\"455\" height=\"211\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/image-5after-getElementByID.png 455w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/image-5after-getElementByID-300x139.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/image-5after-getElementByID-150x70.png 150w\" sizes=\"auto, (max-width: 455px) 100vw, 455px\" \/><\/a><\/p>\n<h2>Changing Style in JavaScript<\/h2>\n<p><strong>Code-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;body&gt;\r\n&lt;h1&gt;JavaScript&lt;\/h1&gt;\r\n&lt;p&gt;JavaScript to change the content of an HTML element:&lt;\/p&gt;\r\n&lt;button type=\"button\" onclick=\"myFunction()\"&gt;Click&lt;\/button&gt;\r\n&lt;p id=\"demo\"&gt;DataFlair&lt;\/p&gt;\r\n&lt;script&gt;\r\nfunction myFunction() {\r\n  document.getElementById(\"demo\").style.fontSize = \"25px\"; \r\n  document.getElementById(\"demo\").style.color = \"white\";\r\n  document.getElementById(\"demo\").style.backgroundColor = \"blue\";        \r\n}\r\n&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p><strong>Output-<\/strong><br \/>\nBefore-<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-6beforechanging-style.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78986\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-6beforechanging-style.png\" alt=\"before changing style\" width=\"455\" height=\"222\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-6beforechanging-style.png 455w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-6beforechanging-style-300x146.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-6beforechanging-style-150x73.png 150w\" sizes=\"auto, (max-width: 455px) 100vw, 455px\" \/><\/a><\/p>\n<p>After-<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-7afterchanging-style.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78987\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-7afterchanging-style.png\" alt=\"after changing style\" width=\"503\" height=\"289\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-7afterchanging-style.png 503w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-7afterchanging-style-300x172.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-7afterchanging-style-150x86.png 150w\" sizes=\"auto, (max-width: 503px) 100vw, 503px\" \/><\/a><\/p>\n<h2>HTML JavaScript Image Attributes (Change HTML Attribute)<\/h2>\n<p><strong>Code-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;body&gt;\r\n&lt;script&gt;\r\nfunction myFun(sw) {\r\n  var pic;\r\n  if (sw == 1) {\r\n    pict = \"images\/river.jpg\"\r\n  } else {\r\n    pict = \"images\/mountain.jpg\"\r\n  }\r\n  document.getElementById('myImage').src = pict;\r\n}\r\n&lt;\/script&gt;\r\n\r\n&lt;img id=\"myImage\" src=\"images\/landscape.jpg\" width=\"100\" height=\"180\"&gt;\r\n\r\n&lt;p&gt;\r\n&lt;button type=\"button\" onclick=\"myFun(1)\"&gt;River&lt;\/button&gt;\r\n&lt;button type=\"button\" onclick=\"myFun(0)\"&gt;Mountain&lt;\/button&gt;\r\n&lt;\/p&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p><strong>Output-<\/strong><br \/>\nBefore-<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-8beforeimage-attributes.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78988\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-8beforeimage-attributes.png\" alt=\"before image attributes\" width=\"240\" height=\"307\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-8beforeimage-attributes.png 240w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-8beforeimage-attributes-235x300.png 235w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-8beforeimage-attributes-117x150.png 117w\" sizes=\"auto, (max-width: 240px) 100vw, 240px\" \/><\/a><\/p>\n<p>After Clicking River-<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-9afterimage-attributesclicking-river.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78989\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-9afterimage-attributesclicking-river.png\" alt=\"after,image attributes,clicking river\" width=\"255\" height=\"330\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-9afterimage-attributesclicking-river.png 255w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-9afterimage-attributesclicking-river-232x300.png 232w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-9afterimage-attributesclicking-river-116x150.png 116w\" sizes=\"auto, (max-width: 255px) 100vw, 255px\" \/><\/a><br \/>\nAfter Clicking Mountain-<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-10afterimage-attributesclicking-mountain.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78990\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-10afterimage-attributesclicking-mountain.png\" alt=\",after,image attributes,clicking mountain\" width=\"243\" height=\"307\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-10afterimage-attributesclicking-mountain.png 243w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-10afterimage-attributesclicking-mountain-237x300.png 237w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Image-10afterimage-attributesclicking-mountain-119x150.png 119w\" sizes=\"auto, (max-width: 243px) 100vw, 243px\" \/><\/a><\/p>\n<h2>JavaScript &lt;noscript&gt; tag<\/h2>\n<p>Some browsers do not support client-side scripts, or client-side scripts have been disabled in them. For such browsers, the &lt;noscript&gt; tag is useful to display alternate text.<br \/>\n<strong>For example,<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;html&gt;\r\n&lt;body&gt;\r\n&lt;h1&gt;JavaScript&lt;\/h1&gt;\r\n&lt;p&gt;JavaScript to change the content of an HTML element:&lt;\/p&gt;\r\n&lt;button type=\"button\" onclick=\"myFunction()\"&gt;Click&lt;\/button&gt;\r\n&lt;p id=\"demo\"&gt;Initially&lt;\/p&gt;\r\n&lt;script&gt;\r\nfunction myFunction() { \r\n  document.getElementById(\"demo\").innerHTML = \"Hello World!\";\r\n}\r\n&lt;\/script&gt;\r\n&lt;noscript&gt;\r\nYour browser does not support JavaScript\r\n&lt;\/noscript&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<h2>Default Scripting Language<\/h2>\n<p>An HTML document may comprise multiple scripts, thus multiple files. HTML5 allows developers to write the script language only once, for these multiple scripts in the &lt;meta&gt; tag . For example,<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;head&gt;\r\n&lt;meta http-equiv = \"Content-Script-Type\" content = \"text\/JavaScript\"&gt;\r\n&lt;\/head&gt;\r\n<\/pre>\n<h2>Summary<\/h2>\n<p>In order to make web-pages interactive and dynamic, we need a scripting language. In this article, we\u2019ve discussed the most popular scripting language, JavaScript. JavaScript is useful for manipulation of images, event-handling, validation of forms, etc. We\u2019ve looked at the advantages of HTML JavaScript, the &lt;script&gt; tag, how to access elements using JavaScript, and a basic taste of JavaScript. We\u2019ve also looked at the event handlers in JavaScript and the &lt;noscript&gt; tag.<span hidden class=\"__iawmlf-post-loop-links\" data-iawmlf-links=\"[{&quot;id&quot;:1055,&quot;href&quot;:&quot;https:\\\/\\\/itconnect.uw.edu\\\/learn\\\/workshops\\\/online-tutorials\\\/web-publishing\\\/javascript&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20220518025509\\\/https:\\\/\\\/itconnect.uw.edu\\\/learn\\\/workshops\\\/online-tutorials\\\/web-publishing\\\/javascript\\\/&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-09 00:07:29&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2025-12-20 17:57:55&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-01-16 23:52:32&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-01-31 15:55:27&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-02-04 10:46:28&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-02-09 17:38:00&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-02-17 10:11:15&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-02-24 13:33:43&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-03-22 06:37:59&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-03-25 23:28:54&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-05-05 18:01:22&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-06-01 02:46:49&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-06-04 12:18:49&quot;,&quot;http_code&quot;:503}],&quot;broken&quot;:true,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-06-04 12:18:49&quot;,&quot;http_code&quot;:503},&quot;process&quot;:&quot;done&quot;}]\"><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>HTML documents can be made interactive and dynamic using a script. A script is a piece of code that adds dynamism to a website. The popular scripting languages are VBScript, JavaScript, Python, etc. Nowadays,&#46;&#46;&#46;<\/p>\n","protected":false},"author":10,"featured_media":78979,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22400],"tags":[22505,22506,22504],"class_list":["post-78932","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-html","tag-add-javascript-file-to-html","tag-html-and-javascript","tag-html-javascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>HTML JavaScript - Add Javascript File to HTML - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn HTML and javascript, their uses &amp; importance, html javascript function, javascript tags list, why add javascript file to html, HTML Events with JavaScript etc\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/html-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"HTML JavaScript - Add Javascript File to HTML - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn HTML and javascript, their uses &amp; importance, html javascript function, javascript tags list, why add javascript file to html, HTML Events with JavaScript etc\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/html-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-04T03:30:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-07-24T11:33:56+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/HTML-JavaScript-df.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"HTML JavaScript - Add Javascript File to HTML - DataFlair","description":"Learn HTML and javascript, their uses & importance, html javascript function, javascript tags list, why add javascript file to html, HTML Events with JavaScript etc","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/html-javascript\/","og_locale":"en_US","og_type":"article","og_title":"HTML JavaScript - Add Javascript File to HTML - DataFlair","og_description":"Learn HTML and javascript, their uses & importance, html javascript function, javascript tags list, why add javascript file to html, HTML Events with JavaScript etc","og_url":"https:\/\/data-flair.training\/blogs\/html-javascript\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2020-07-04T03:30:34+00:00","article_modified_time":"2024-07-24T11:33:56+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/HTML-JavaScript-df.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/html-javascript\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/html-javascript\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/a90b082e16aa38d207212d22b0581f33"},"headline":"HTML JavaScript &#8211; Add Javascript File to HTML","datePublished":"2020-07-04T03:30:34+00:00","dateModified":"2024-07-24T11:33:56+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/html-javascript\/"},"wordCount":769,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/html-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/HTML-JavaScript-df.jpg","keywords":["add javascript file to html","html and javascript","HTML JavaScript"],"articleSection":["HTML Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/html-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/html-javascript\/","url":"https:\/\/data-flair.training\/blogs\/html-javascript\/","name":"HTML JavaScript - Add Javascript File to HTML - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/html-javascript\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/html-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/HTML-JavaScript-df.jpg","datePublished":"2020-07-04T03:30:34+00:00","dateModified":"2024-07-24T11:33:56+00:00","description":"Learn HTML and javascript, their uses & importance, html javascript function, javascript tags list, why add javascript file to html, HTML Events with JavaScript etc","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/html-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/html-javascript\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/html-javascript\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/HTML-JavaScript-df.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/HTML-JavaScript-df.jpg","width":1200,"height":628,"caption":"HTML JavaScript"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/html-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"HTML Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/html\/"},{"@type":"ListItem","position":3,"name":"HTML JavaScript &#8211; Add Javascript File to HTML"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/a90b082e16aa38d207212d22b0581f33","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/dd6de0d647a0185cd6faf264e4ba860b0d85d08d7070766f9cd41bea5bb0b227?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/dd6de0d647a0185cd6faf264e4ba860b0d85d08d7070766f9cd41bea5bb0b227?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dd6de0d647a0185cd6faf264e4ba860b0d85d08d7070766f9cd41bea5bb0b227?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team is passionate about delivering top-notch tutorials and resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With expertise in the tech industry, we simplify complex topics to help learners excel. Stay updated with our latest insights.","url":"https:\/\/data-flair.training\/blogs\/author\/dfadteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/78932","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=78932"}],"version-history":[{"count":5,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/78932\/revisions"}],"predecessor-version":[{"id":142897,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/78932\/revisions\/142897"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/78979"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=78932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=78932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=78932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}