{"id":6744,"date":"2021-04-22T12:51:00","date_gmt":"2021-04-22T11:51:00","guid":{"rendered":"https:\/\/www.bronco.co.uk\/our-ideas\/?p=6744"},"modified":"2021-04-07T16:52:08","modified_gmt":"2021-04-07T15:52:08","slug":"creating-a-generic-ajax-tab-replace-function","status":"publish","type":"post","link":"https:\/\/www.bronco.co.uk\/our-ideas\/creating-a-generic-ajax-tab-replace-function\/","title":{"rendered":"Creating a generic AJAX tab\/replace function"},"content":{"rendered":"\n<p>Recently, when starting out on new projects I\u2019ve been working to ensure that the code I write is more generic so that it is reusable throughout both the initial build of the project as well as in the future.<\/p>\n\n\n\n<p>It\u2019s easy, especially when writing JavaScript\/jQuery, to write code for a section that targets specific class or id selectors that then makes it more difficult to use this code elsewhere in your website.<\/p>\n\n\n\n<p>Previously I\u2019ve created a <a href=\"https:\/\/www.bronco.co.uk\/our-ideas\/creating-a-generic-function-to-show-or-hide-elements-in-javascript\/\">generic show\/hide function<\/a> for features such as drop-down menus. This time I wanted to tackle replacement content, such as that you might see in a tabbed section.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Use-case<\/h2>\n\n\n\n<p>When rebuilding the homepage for a long-term client one of our goals was to improve the loading speed of the website, and reduce the overall page weight.<\/p>\n\n\n\n<p>The page included third-party widgets used to display Instagram and Youtube playlists that could not be replaced or edited to make them less impactful to the speed of the website. Removing them also wasn\u2019t an option.<\/p>\n\n\n\n<p>But we could look at delaying their inclusion on the page thus reducing their impact on a number of speed metrics.<\/p>\n\n\n\n<p>To do this we would place the widgets within a tabbed section. Traditionally this may mean that the widgets are still present in the code and so some, if not all, assets would be loaded even though they\u2019re not in view. To ensure this isn\u2019t the case we would instead utilise AJAX loading to request the content only on user selection; ensuring the page is lightweight on initial load as the widget would not be anywhere in the code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Building a generic script to replace the tabbed content<\/h2>\n\n\n\n<p>Here is our example code. When the relevant tab is clicked the jQuery makes the request tot he server and new content replaces the existing tabbed content.<\/p>\n\n\n\n<p class=\"codepen\" data-height=\"565\" data-theme-id=\"dark\" data-default-tab=\"js,result\" data-user=\"keanr\" data-slug-hash=\"KKaXgKJ\" style=\"height: 565px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;\" data-pen-title=\"Replace Tabs\">\n  <span>See the Pen <a href=\"https:\/\/codepen.io\/keanr\/pen\/KKaXgKJ\">\n  Replace Tabs<\/a> by Kean Richmond (<a href=\"https:\/\/codepen.io\/keanr\">@keanr<\/a>)\n  on <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<\/span>\n<\/p>\n<script async src=\"https:\/\/cpwebassets.codepen.io\/assets\/embed\/ei.js\"><\/script>\n\n\n\n<p>As you\u2019ll see we target the data-replace attribute which can be placed on an individual <code>&lt;a><\/code> element or on a parent container as shown in the example.<\/p>\n\n\n\n<p class=\"has-small-font-size\">Note: In this instance we\u2019ve used jQuery, but there is nothing here that could not be easily achieved with vanilla JavaScript.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The ideal solution?<\/h2>\n\n\n\n<p>This worked great, but in our specific case there was an issue. With tabbed content it\u2019s common for new users to click around the tabs to view the content present, before returning to what they might actually wish to view.<\/p>\n\n\n\n<p>While any request back to the server could appear slow to some users, this was especially obvious with the widgets where, after being added to the page, would subsequently request a number of assets from a third party. This results in a much more obvious delay when clicking around the various tabs.<\/p>\n\n\n\n<p>What we needed was a solution where new content would be appended to the page, and for this to be made visible on subsequent views rather than reloading the content each time.<\/p>\n\n\n\n<p class=\"codepen\" data-height=\"565\" data-theme-id=\"dark\" data-default-tab=\"js,result\" data-user=\"keanr\" data-slug-hash=\"xxgXrKm\" style=\"height: 565px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;\" data-pen-title=\"Append Tabs\">\n  <span>See the Pen <a href=\"https:\/\/codepen.io\/keanr\/pen\/xxgXrKm\">\n  Append Tabs<\/a> by Kean Richmond (<a href=\"https:\/\/codepen.io\/keanr\">@keanr<\/a>)\n  on <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<\/span>\n<\/p>\n<script async src=\"https:\/\/cpwebassets.codepen.io\/assets\/embed\/ei.js\"><\/script>\n\n\n\n<p class=\"has-small-font-size\">Note: You may need to open dev tools to confirm that the newly added content remains in the code, rather than replaced as occurs on the example furth up this article<\/p>\n\n\n\n<p>While the original replace only function allows PHP to be used to determine the current state of the tabs (as these can be replaced alongside the content), with the append function we\u2019re having to update the current state of the tabs as well as the content that is shown within our JavaScript function.<\/p>\n\n\n\n<p>Though this potentially makes the function somewhat less generic it\u2019s only by finding other cases where we might want to use such a function that we can determine what is specific to the implementation we\u2019ve tested and what changes are necessary to make things more versatile.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The right mindset<\/h2>\n\n\n\n<p>It can be easy at the start of a project to code up something quick for a particular purpose. But as the project grows it\u2019s likely that you\u2019ll find yourself writing similar code that already exists. You could revisit the original code but there\u2019s always the concern you may cause something to break that you hadn\u2019t accounted for.<\/p>\n\n\n\n<p>Instead starting from a more generic mindset at the start of project helps ensure that the code you\u2019re writing is more versatile, not just on this project but potentially future projects too.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently, when starting out on new projects I\u2019ve been working to ensure that the code I write is more generic so that it is reusable throughout both the initial build of the project as well as in the future. It\u2019s easy, especially when writing JavaScript\/jQuery, to write code for a section that targets specific class [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":6746,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"class_list":["post-6744","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-and-ux"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts\/6744","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/comments?post=6744"}],"version-history":[{"count":0,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts\/6744\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/media\/6746"}],"wp:attachment":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/media?parent=6744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/categories?post=6744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}