Logo

dev-resources.site

for different kinds of informations.

How to Use JavaScript to Reduce HTML Code: A Simple Example

Published at
1/15/2025
Categories
webdev
javascript
html
tutorial
Author
egroene
Categories
4 categories in total
webdev
open
javascript
open
html
open
tutorial
open
Author
7 person written this
egroene
open
How to Use JavaScript to Reduce HTML Code: A Simple Example

A huge part of being a good programmer is keeping your code as easy-to-debug as possible, as well as writing code that is easy to scale without making mistakes. If youā€™re working on, say, a blog website, the amount of content on the site will grow over time, so every time you come back to the project, you want to be able to do so quickly & without making mistakes, which could harm your clientā€™s reputation by making the site look bad.

Luckily for us, God (or rather, Brendan Eich) created JavaScript, which can help make our code more efficient & easier to debug. In this article, I will give a brief example of how I used JS to do exactly this while building a blog website.

On a typical blog website, you should make it easy for visitors to find you on social media, as this is the best way to grow a following online. One way to do this is to display links to your social media channels on every page, maybe in a footer, or in a panel that sits on the side of the screen at all times, as seen in the photo below:

Panel with social links (encircled)

If you want this panel to display on all pages of the site, one way to accomplish this would be to copy & paste several lines (14 in this case) of the same code onto every HTML page in the project. These lines are pictured below:

HTML to create the social media panel, as seen in the first picture in this article

Fourteen lines of code may not seem like much to you, and, in a way, youā€™re right. It really wouldnā€™t be that difficult to just copy & paste these onto every new HTML page you create, especially if the project is small. However, it makes the code clunkier, meaning the page wonā€™t load as soon as it would with fewer lines, as well as more difficult to modify or debug. In the photo above, I put hashtags in the href part of the anchor tags, as this is only a demo.

However, in a realistic situation, these would link to a real social media profileā€™s URL. So, imagine your client changes their Instagram username. This could lead to the URL, that is put into href, being different than it was before. As the developer, you would have to get that new link & paste it into every single HTML page & in the appropriate spot. Not only is this cumbersome, but it could lead to errors if you are not careful, especially if you get bored doing this repetitive task.

A better way to accomplish this would be to use JS. In the picture below, I created functionality in JS to add these links to a div that is already present on all HTML pages, which is stored in the variable socialsPanel.

Functionality to populate the social panelā€™s HTML

When we use a function like this, we only need to enter each linkā€™s information in one place: inside this function. So, if the information for a particular link changes, we would only need to change it here, instead of on every single HTML page if we hadnā€™t used JS & had instead used the copy/paste method. If we wanted to add or subtract links, we would only have to do so in this function.

Not only is this a huge timesaver, but, as mentioned before, it saves lines of HTML code, since to populate a page with these links, we wouldnā€™t need to copy & paste the 13* lines of code into every HTML document, only call the function upon loading the page. Calling this function, popSocialsPanel(), on a page, as you can see underlined in red in the next photo, adds no new line of code (just ignore the other functions there for now):

Lines that contain the series of links in this case, since we kept the div , to which we wanted to add the links, on each HTML document

Call function upon loading of a page where you want the social media panel to display

Letā€™s take a minute to do the math on all of this. Not including the comments, we added 9 new lines of code in our JS file, which we only have to write once, while subtracting 13 lines of HTML code. So, if our project had only one HTML document, we would still be saving 4 lines of code, which is good, but we would be saving 13 lines of code while implementing this functionality for every HTML document we add to the project, which is great.

On this particular project, I have saved 56 lines of code, so far, by using this method. For every page for a blog post I add, I will be saving another 13.

I hope you found this relatively simple example helpful, but I guess the takeaway from this article would be that you should develop a certain instinct as a programmer; if you feel that you are repeating yourself while writing code, there is probably a more-efficient way to write it.

If you have any comments or questions, Iā€™d be happy to respond. If you found this article helpful, Iā€™d appreciate a nice comment or a clap or two, so I can know what content my readers like. If you think others would find this useful, please share it with them.

Thanks for reading!

Originally published on Medium for JavaScript in Plain English on September 28, 2022

html Article's
30 articles in total
Favicon
Learning HTML is the best investment I ever did
Favicon
[Boost]
Favicon
How to Use JavaScript to Reduce HTML Code: A Simple Example
Favicon
got Tired of analysis paralyysis so i built an extensioon to get into flow faster
Favicon
Truncating Text with Text-Overflow
Favicon
Label + Checkbox States
Favicon
Getting Started with HTML
Favicon
Create Stunning Gradual Div Reveals with JavaScript setTimeout and CSS Transitions
Favicon
3D models
Favicon
Useful CSS Selectors You Might Not Know
Favicon
[Boost]
Favicon
Building the Foundations: A Beginnerā€™s Guide to HTML
Favicon
Shadow DOM Perfected: Experience the Power of Monster 3.100!
Favicon
Rendering Shopify Liquid Code Locally with VS Code
Favicon
Top HTML Interview Questions and Answers
Favicon
Files Paths & Images
Favicon
Reading Progress Bar
Favicon
[Boost]
Favicon
šŸˆā€ā¬› Git and GitHub: A Beginnerā€™s Guide to Version Control šŸš€
Favicon
Using the currentColor property to change SVG color
Favicon
Introduction to HTML Elements
Favicon
Animated Gradient Background
Favicon
HTML Validation
Favicon
Seven quickest ways to center your div using CSS
Favicon
10 Common HTML and CSS Interview Questions and Answers
Favicon
HTML Semantic tags
Favicon
Making Video Creation Easy? InVideoAI.video Has the Answer
Favicon
Gallery with varied image sizes using aspect-ratio and object-fit
Favicon
Adaptive va Repsonsive dizayn farqi
Favicon
Static website forms

Featured ones: