Paul Bradley • Solutions Architect & Software Developer


Published:

Frontend Tips and Tools

A collection of tips and tools that I use my web applications which enhance the frontend development

midjourney ai - a fantasy rhino with crystal horns

Table of Contents
  1. Preventing browsers auto-completing a field
  2. Preventing browsers from trying to translate the web page to another language
  3. Define a blank favicon within your HTML head
  4. Validating HTML
  5. Preventing users opening developer tools
  6. Preventing users printing your applications content
  7. Joined bullet point lists with CSS
  8. Send AJAX request as a HTTP POST with an Authorization Request Header

 Preventing browsers auto-completing a field

Sometimes it’s important to not show the user previous search values. When you need a distraction free input field, so the user can concentrate on the data they are currently entering, use the autocomplete attribute of an HTML field and set the value to off.

<input id="q" name="q" size="12" minlength="10" maxlength="10" required autofocus autocomplete="off">

 Preventing browsers from trying to translate the web page to another language

If you need to prevent browsers from trying to auto translate you web page into another language, and you’ve already the lang attribute then set the translate attribute to no.

<!DOCTYPE html>
<html lang="en" translate="no">

 Define a blank favicon within your HTML head

If your web application doesn’t have a specific favicon and you want to prevent lots of 404 errors within your server log files, then include an in-line blank favicon like:

<link href="data:image/x-icon;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQEAYAAABPYyMiAAAABmJLR0T///////8JWPfcAAAACXBIWXMAAABIAAAASABGyWs+AAAAF0lEQVRIx2NgGAWjYBSMglEwCkbBSAcACBAAAeaR9cIAAAAASUVORK5CYII=" rel="icon" type="image/x-icon" />

 Validating HTML

This seems to have dropped out of fashion. I don’t hear web developers talking about it much. I still validate the HTML my web applications produce to ensure it’s valid markup. You can check that this page is valid

 Preventing users opening developer tools

If you want to discourage users viewing your source HTML or opening the developer tools then look at using disable-devtool. While it won’t prevent the most technical savvy users who know how to request your page from curl, it will prevent the majority of non-technical users. I use a feature flag within my code to enable this within pre-production and production environments.

 Preventing users printing your applications content

As I mainly develop web applications for the health sector, I need to try and prevent or persuade them not print the pages as they might contain patient identifiable information. To do this I use a print style sheet to hide all the content except the content within the pages footer.

The footer contains the message to persuade users not to print the information like:

<footer>
    <p>We <b>strongly recommend</b> not printing patient information from this application as
     it is constantly being updated by many organisations and it will be out of date very quickly.
    Please always consume (view) the latest information about your patient so that you are
    delivering safe and informed care based on the most up to date information available.</p>
    <hr />
    <p>Please <b>DO NOT</b> take photos of the screen as this will break the terms of your employment.</p>
</footer>

A sample print style sheet is shown below. It hides the main content and shows the footer content. The font size of the text within the footer content is bumped up so that it’s easily readable within the browsers print pre-view window.

@media print {
    main {
        display: none;
    }
    footer {
        display: block;
        font-size: 22px !important;
        margin: 2em;
    }
}

 Joined bullet point lists with CSS

I like the joined bullet point lists with CSS first developed by the BBC. It’s a great way to show a vertical timeline of events. Here’s my take on it:

li {
    position: relative;
    margin: 0;
    padding-bottom: .75em;
    padding-left: 20px;
}

li:before {
    content: '';
    background-color: #00669f;
    position: absolute;
    bottom: 0;
    top: 0;
    left: 6px;
    width: 3px;
}

li:after {
    content: '';
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' aria-hidden='true' viewBox='0 0 32 32' focusable='false'%3E%3Ccircle stroke='none' fill='%2300669f' cx='16' cy='16' r='10'%3E%3C/circle%3E%3C/svg%3E");
    position: absolute;
    left: 0;
    top: 2px;
    height: 15px;
    width: 15px;
}

And the HTML:

<ol>
    <li>22.08.2022 - Change Grow Live go-live with 50+ clinical staff</li>
    <li>18.08.2022 - Passed 10,000 registered users</li>
    <li>25.07.2022 - Delivered the Cyted Results uploading and publishing functionality</li>
</ol>

 Send AJAX request as a HTTP POST with an Authorization Request Header

Make your AJAX API request more secure by sending as an HTTP POST with an Authorization request header that the web server needs to verify before processing your request.

try {
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
            // use the API's response
            console.log(xmlHttp.responseText);
        }
        if (xmlHttp.readyState == 4 && xmlHttp.status != 200) {
            console.error(xmlHttp.status);
        }
    }

    xmlHttp.open("POST", "/url/to/api", true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader('Authorization', 'BearerTokenValue');
    xmlHttp.send("field=value")
} catch(e) {
    console.error(e.message);
}