{"id":297,"date":"2014-04-01T10:17:04","date_gmt":"2014-04-01T16:17:04","guid":{"rendered":"https:\/\/tylerfindlay.com\/?p=297"},"modified":"2014-04-01T10:17:04","modified_gmt":"2014-04-01T16:17:04","slug":"simple-optimization-to-make-your-website-faster","status":"publish","type":"post","link":"https:\/\/tylerfindlay.com\/index.php\/2014\/04\/01\/simple-optimization-to-make-your-website-faster\/","title":{"rendered":"Simple Optimization to make your Website Faster"},"content":{"rendered":"<p dir=\"ltr\">Since my first course in Javascript 5 years ago, I was taught like a good web developer that you should always put your javascript declarations in the header tag of your HTML page. The reasoning behind this is quite logical. If you\u2019re attaching and element events to functionality (such as a buttons \u201conclick\u201d event) you want that functionality defined before a user sees the button.<\/p>\n<p><!--more--><\/p>\n<p dir=\"ltr\">This tradition is starting to break though. More recently, we are wrapping all the javascript of our page in a large \u201conload\u201d event (or in jQuery, the $(function() { .. }) to ensure the DOM loads before. We attach events to buttons <i>after\u00a0<\/i>the page is loaded. The reasoning behind this is it allows the user to see the page starting to load more quicky, even if functionality isn\u2019t available right away. So, if we\u2019re waiting on the onload even anyways, then why isn\u2019t loading external javascript files the last thing we do anyways?<\/p>\n<h2>All HTML Page Types<\/h2>\n<p dir=\"ltr\">This is a simple change you can apply to your websites right now. Try it! Take all the javascript declarations and put them as the <i>last\u00a0<\/i>thing that your page loads. If you have and &lt;script&gt; tags throughout your application that run onload, you won\u2019t need to move anything because they will naturally run after the javascript libraries load (however, if you are using jQuery and have encapsulated code in the $(function() { \u2026 } declaration, this will of course need to be moved <i>after\u00a0<\/i>your javascript imports).<\/p>\n<p dir=\"ltr\">Your page content should now load before functionality giving you a boost in speed <i>and\u00a0<\/i>give the user a more responsive looking website.<\/p>\n<h2>Razor Pages<\/h2>\n<p dir=\"ltr\">Razor has simplified this process even more with their _Layout pages. Using \u201cRenderSection\u201d, for each of your content pages you can pull all the pages javascript together and put it in a section called scripts.<\/p>\n<p><strong>index.cshtml<\/strong><\/p>\n<p>[html]<br \/>\n&lt;!&#8211; HTML Content here &#8211;&gt;<br \/>\n@section scripts<br \/>\n{<br \/>\n&lt;script type=&quot;text\/javascript&quot;&gt;<br \/>\n\t\/\/ javascript here<br \/>\n&lt;\/script&gt;<br \/>\n}<br \/>\n[\/html]<\/p>\n<p dir=\"ltr\">Then, you simply modify your _Layout.cshtml page like so.<\/p>\n<p><strong>_Layout.cshtml<\/strong><br \/>\n[html]<br \/>\n&lt;!&#8211; Header content &#8211;&gt;<br \/>\n&lt;body&gt;<br \/>\n@RenderBody()<br \/>\n@Scripts.Render(\u201c~bundles\/jquery\u201d)<br \/>\n@RenderSection(\u201cscripts\u201d, required: false)<br \/>\n&lt;\/body&gt;<br \/>\n[\/html]<\/p>\n<h2>Caution<\/h2>\n<p dir=\"ltr\">If you\u2019re implementing this on an existing site, you might have to do some thorough testing to make sure none of your scripts are running before the declarations. For simpler sites, or ones that have been well coded without heavy reliance on external libraries (jQuery, bootstrap, etc \u2026) you should have no problem applying this paradigm to your pages.<\/p>\n<p dir=\"ltr\">For anyone coding new websites, code your pages loading external javascript upfront!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since my first course in Javascript 5 years ago, I was taught like a good web developer that you should always put your javascript declarations in the header tag of your HTML page. The reasoning behind this is quite logical. If you\u2019re attaching and element events to functionality (such as a buttons \u201conclick\u201d event) you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-297","post","type-post","status-publish","format-standard","hentry","category-technology"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.11 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Simple Optimization to make your Website Faster - Tyler Findlay<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/tylerfindlay.com\/index.php\/2014\/04\/01\/simple-optimization-to-make-your-website-faster\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Simple Optimization to make your Website Faster - Tyler Findlay\" \/>\n<meta property=\"og:description\" content=\"Since my first course in Javascript 5 years ago, I was taught like a good web developer that you should always put your javascript declarations in the header tag of your HTML page. The reasoning behind this is quite logical. If you\u2019re attaching and element events to functionality (such as a buttons \u201conclick\u201d event) you [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/tylerfindlay.com\/index.php\/2014\/04\/01\/simple-optimization-to-make-your-website-faster\/\" \/>\n<meta property=\"og:site_name\" content=\"Tyler Findlay\" \/>\n<meta property=\"article:published_time\" content=\"2014-04-01T16:17:04+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/tylerfindlay.com\/index.php\/2014\/04\/01\/simple-optimization-to-make-your-website-faster\/\",\"url\":\"https:\/\/tylerfindlay.com\/index.php\/2014\/04\/01\/simple-optimization-to-make-your-website-faster\/\",\"name\":\"Simple Optimization to make your Website Faster - Tyler Findlay\",\"isPartOf\":{\"@id\":\"https:\/\/tylerfindlay.com\/#website\"},\"datePublished\":\"2014-04-01T16:17:04+00:00\",\"dateModified\":\"2014-04-01T16:17:04+00:00\",\"author\":{\"@id\":\"https:\/\/tylerfindlay.com\/#\/schema\/person\/53a67884f40536baaa37a6e37e404273\"},\"breadcrumb\":{\"@id\":\"https:\/\/tylerfindlay.com\/index.php\/2014\/04\/01\/simple-optimization-to-make-your-website-faster\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/tylerfindlay.com\/index.php\/2014\/04\/01\/simple-optimization-to-make-your-website-faster\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/tylerfindlay.com\/index.php\/2014\/04\/01\/simple-optimization-to-make-your-website-faster\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/tylerfindlay.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Simple Optimization to make your Website Faster\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/tylerfindlay.com\/#website\",\"url\":\"https:\/\/tylerfindlay.com\/\",\"name\":\"Tyler Findlay\",\"description\":\"Computer Scientist. Perpetual Vagabond. Technology whore. Leadership enthusiast. Liker of funny stuff. Would make an outstanding Jedi.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/tylerfindlay.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/tylerfindlay.com\/#\/schema\/person\/53a67884f40536baaa37a6e37e404273\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/tylerfindlay.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e964dc4307e6f3c229ac696c302fd293754245cc427c34e375eab35ec671b5d6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e964dc4307e6f3c229ac696c302fd293754245cc427c34e375eab35ec671b5d6?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"sameAs\":[\"https:\/\/temp.tylerfindlay.com\"],\"url\":\"https:\/\/tylerfindlay.com\/index.php\/author\/tyler_d1ogunpg\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Simple Optimization to make your Website Faster - Tyler Findlay","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:\/\/tylerfindlay.com\/index.php\/2014\/04\/01\/simple-optimization-to-make-your-website-faster\/","og_locale":"en_US","og_type":"article","og_title":"Simple Optimization to make your Website Faster - Tyler Findlay","og_description":"Since my first course in Javascript 5 years ago, I was taught like a good web developer that you should always put your javascript declarations in the header tag of your HTML page. The reasoning behind this is quite logical. If you\u2019re attaching and element events to functionality (such as a buttons \u201conclick\u201d event) you [&hellip;]","og_url":"https:\/\/tylerfindlay.com\/index.php\/2014\/04\/01\/simple-optimization-to-make-your-website-faster\/","og_site_name":"Tyler Findlay","article_published_time":"2014-04-01T16:17:04+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/tylerfindlay.com\/index.php\/2014\/04\/01\/simple-optimization-to-make-your-website-faster\/","url":"https:\/\/tylerfindlay.com\/index.php\/2014\/04\/01\/simple-optimization-to-make-your-website-faster\/","name":"Simple Optimization to make your Website Faster - Tyler Findlay","isPartOf":{"@id":"https:\/\/tylerfindlay.com\/#website"},"datePublished":"2014-04-01T16:17:04+00:00","dateModified":"2014-04-01T16:17:04+00:00","author":{"@id":"https:\/\/tylerfindlay.com\/#\/schema\/person\/53a67884f40536baaa37a6e37e404273"},"breadcrumb":{"@id":"https:\/\/tylerfindlay.com\/index.php\/2014\/04\/01\/simple-optimization-to-make-your-website-faster\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/tylerfindlay.com\/index.php\/2014\/04\/01\/simple-optimization-to-make-your-website-faster\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/tylerfindlay.com\/index.php\/2014\/04\/01\/simple-optimization-to-make-your-website-faster\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/tylerfindlay.com\/"},{"@type":"ListItem","position":2,"name":"Simple Optimization to make your Website Faster"}]},{"@type":"WebSite","@id":"https:\/\/tylerfindlay.com\/#website","url":"https:\/\/tylerfindlay.com\/","name":"Tyler Findlay","description":"Computer Scientist. Perpetual Vagabond. Technology whore. Leadership enthusiast. Liker of funny stuff. Would make an outstanding Jedi.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/tylerfindlay.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/tylerfindlay.com\/#\/schema\/person\/53a67884f40536baaa37a6e37e404273","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/tylerfindlay.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e964dc4307e6f3c229ac696c302fd293754245cc427c34e375eab35ec671b5d6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e964dc4307e6f3c229ac696c302fd293754245cc427c34e375eab35ec671b5d6?s=96&d=mm&r=g","caption":"admin"},"sameAs":["https:\/\/temp.tylerfindlay.com"],"url":"https:\/\/tylerfindlay.com\/index.php\/author\/tyler_d1ogunpg\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/tylerfindlay.com\/index.php\/wp-json\/wp\/v2\/posts\/297","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tylerfindlay.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tylerfindlay.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tylerfindlay.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tylerfindlay.com\/index.php\/wp-json\/wp\/v2\/comments?post=297"}],"version-history":[{"count":0,"href":"https:\/\/tylerfindlay.com\/index.php\/wp-json\/wp\/v2\/posts\/297\/revisions"}],"wp:attachment":[{"href":"https:\/\/tylerfindlay.com\/index.php\/wp-json\/wp\/v2\/media?parent=297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tylerfindlay.com\/index.php\/wp-json\/wp\/v2\/categories?post=297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tylerfindlay.com\/index.php\/wp-json\/wp\/v2\/tags?post=297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}