{"id":2192,"date":"2022-04-12T16:00:40","date_gmt":"2022-04-12T16:00:40","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=2192"},"modified":"2022-04-12T16:00:40","modified_gmt":"2022-04-12T16:00:40","slug":"external-javascript-file","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/04\/12\/external-javascript-file\/","title":{"rendered":"External JavaScript file"},"content":{"rendered":"\n<p>We can create external JavaScript file and embed it in many html page.<\/p>\n\n\n\n<p>It provides&nbsp;<strong>code re usability<\/strong>&nbsp;because single JavaScript file can be used in several html pages.<\/p>\n\n\n\n<p>An external JavaScript file must be saved by .js extension. It is recommended to embed all JavaScript files into a single file. It increases the speed of the webpage.<\/p>\n\n\n\n<p>Let&#8217;s create an external&nbsp;<a href=\"https:\/\/www.javatpoint.com\/javascript-tutorial\">JavaScript<\/a>&nbsp;file that prints Hello Javatpoint in a alert dialog box.<\/p>\n\n\n\n<p><strong>message.js<\/strong><a href=\"https:\/\/www.javatpoint.com\/external-javascript-file#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/external-javascript-file#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/external-javascript-file#\"><\/a><\/p>\n\n\n\n<ol><li>function&nbsp;msg(){&nbsp;&nbsp;<\/li><li class=\"\">&nbsp;alert(&#8220;Hello&nbsp;Javatpoint&#8221;);&nbsp;&nbsp;<\/li><li>}&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p>Let&#8217;s include the JavaScript file into&nbsp;<a href=\"https:\/\/www.javatpoint.com\/html-tutorial\">html<\/a>&nbsp;page. It calls the&nbsp;<a href=\"https:\/\/www.javatpoint.com\/javascript-function\">JavaScript function<\/a>&nbsp;on button click.<\/p>\n\n\n\n<p><strong>index.html<\/strong><a href=\"https:\/\/www.javatpoint.com\/external-javascript-file#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/external-javascript-file#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/external-javascript-file#\"><\/a><\/p>\n\n\n\n<ol><li><strong>&lt;<\/strong><strong>html<\/strong><strong>&gt;<\/strong>&nbsp;&nbsp;<\/li><li class=\"\"><strong>&lt;<\/strong><strong>head<\/strong><strong>&gt;<\/strong>&nbsp;&nbsp;<\/li><li><strong>&lt;<\/strong><strong>script<\/strong>&nbsp;type=&#8221;text\/javascript&#8221;&nbsp;src=&#8221;message.js&#8221;<strong>&gt;<\/strong><strong>&lt;\/<\/strong><strong>script<\/strong><strong>&gt;<\/strong>&nbsp;&nbsp;<\/li><li class=\"\"><strong>&lt;\/<\/strong><strong>head<\/strong><strong>&gt;<\/strong>&nbsp;&nbsp;<\/li><li><strong>&lt;<\/strong><strong>body<\/strong><strong>&gt;<\/strong>&nbsp;&nbsp;<\/li><li class=\"\"><strong>&lt;<\/strong><strong>p<\/strong><strong>&gt;<\/strong>Welcome&nbsp;to&nbsp;JavaScript<strong>&lt;\/<\/strong><strong>p<\/strong><strong>&gt;<\/strong>&nbsp;&nbsp;<\/li><li><strong>&lt;<\/strong><strong>form<\/strong><strong>&gt;<\/strong>&nbsp;&nbsp;<\/li><li class=\"\"><strong>&lt;<\/strong><strong>input<\/strong>&nbsp;type=&#8221;button&#8221;&nbsp;value=&#8221;click&#8221;&nbsp;onclick=&#8221;msg()&#8221;<strong>\/&gt;<\/strong>&nbsp;&nbsp;<\/li><li><strong>&lt;\/<\/strong><strong>form<\/strong><strong>&gt;<\/strong>&nbsp;&nbsp;<\/li><li class=\"\"><strong>&lt;\/<\/strong><strong>body<\/strong><strong>&gt;<\/strong>&nbsp;&nbsp;<\/li><li><strong>&lt;\/<\/strong><strong>html<\/strong><strong>&gt;<\/strong>&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of External JavaScript<\/h2>\n\n\n\n<p>There will be following benefits if a user creates an external javascript:<\/p>\n\n\n\n<ol><li>It helps in the reusability of code in more than one HTML file.<\/li><li>It allows easy code readability.<\/li><li>It is time-efficient as web browsers cache the external js files, which further reduces the page loading time.<\/li><li>It enables both web designers and coders to work with html and js files parallelly and separately, i.e., without facing any code conflictions.<\/li><li>The length of the code reduces as only we need to specify the location of the js file.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Disadvantages of External JavaScript<\/h2>\n\n\n\n<p>There are the following disadvantages of external files:<\/p>\n\n\n\n<ol><li>The stealer may download the coder&#8217;s code using the url of the js file.<\/li><li>If two js files are dependent on one another, then a failure in one file may affect the execution of the other dependent file.<\/li><li>The web browser needs to make an additional http request to get the js code.<\/li><li>A tiny to a large change in the js code may cause unexpected results in all its dependent files.<\/li><li>We need to check each file that depends on the commonly created external javascript file.<\/li><li>If it is a few lines of code, then better to implement the internal javascript code.<\/li><\/ol>\n","protected":false},"excerpt":{"rendered":"<p>We can create external JavaScript file and embed it in many html page. It provides&nbsp;code re usability&nbsp;because single JavaScript file can be used in several html pages. An external JavaScript file must be saved by .js extension. It is recommended to embed all JavaScript files into a single file. It increases the speed of the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[429],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/2192"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=2192"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/2192\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=2192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=2192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=2192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}