Urgent.News

What's breaking now, across thousands of outlets.

Tech

Inline rendering with document.currentScript

For simple dynamic content rendering, you might not need advanced templating languages or PHP backends. The example phrase, "Come on, it’s currentYear", should render as "Come on, it’s" today. Instead of using IDs, classes, or querySelectors in your JavaScript, you can refer to the running script element directly using the document.currentScript property. The dynamic phrase "Come on, it’s" can then be written within the script as follows.

The code might seem wordy, but it can be aliased to a constant like $ via $=(...n)= document.currentScript.replaceWith(...n). This allows for a more readable and template-literal-like approach, even without explicit context.

This is the RenderJS custom element, which simply replaces itself with the result of its contents treated as JavaScript code. To use it, you must first name the tag, using an emoji (💲) as a workaround for the dollar sign ($) not being allowed in tag names. The name becomes j-💲, representing JavaScript.

When employing this technique, it's recommended to include a noscript fallback for those who don't run JavaScript. This method may seem useful only for rendering the current year, but it can be applied to various scenarios, such as displaying relative dates, generating markup per character, or creating text animations.

Unlike templating languages, you cannot perform data-driven templating using inline scripts. This is because data must be pre-injected into the global scope, and fetching data through inline scripts incurs an extra HTTP request, leading to potential performance issues. However, for personal projects, direct patching of data-driven HTML can be an effective alternative to traditional templating systems.

Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at leanrada.com →

More in Tech

How Browser Games Work: HTML5, Canvas and WebGL Explained

Browser games have changed dramatically over the years. Older web games often depended on plugins such as Flash, but modern browser games can now run directly inside Chrome, Firefox, Safari, Edge and…

  • Browser games run directly in web browsers using HTML5, Canvas, WebGL
  • HTML5 provides foundational structure, Canvas renders graphics, WebGL enables 2D/3D graphics
  • WebSockets and WebAssembly facilitate multiplayer and complex computations

The Provenance Layer — Or, You Can't Audit What You Can't Rebuild

Yesterday was the solvency layer (#63): the vendor holding your switch can run out of money. Today's front page answers the next question down — how do you know what the thing on your machine actually…

  • Provenance Layer addresses authenticity of software dependencies
  • Most dependencies cannot be rebuilt from public inputs
  • Implement provenance by pinning dependencies by digest and using lockfiles

Bad Habits in Web Development We Should Avoid

Web development mixes creativity, technology and user experience. It's also easy to pick up habits that quietly erode a site's quality, accessibility and maintainability.

  • Neglecting mobile design from the outset degrades site quality and accessibility
  • Inconsistent design elements confuse users and give unprofessional impression
  • Hardcoding URLs makes website maintenance and scaling difficult

A dark cloud hanging over Nigeria

Distinguished guests, ladies and gentlemen. Some months ago, a colleague walked into my office and noticed the back issues of Nigerian newspapers and magazines, together with printed copies of…

More from Monday 19 October →