Manage and format Dates with JavaScript

Update (2025): This article was written in 2013. It still offers useful basics, but a few notes: Date.getMonth() is zero‑based (January = 0). Date.getDay() returns the weekday (0–6), not the day of month. There is no setDay() or setUTCDay() method in JavaScript. Moment.js is in maintenance mode; modern alternatives include Luxon, date‑fns, and native Intl.DateTimeFormat. Consider these for new projects. JavaScript has a Date object you can use to handle dates. Supported constructors are: ...

November 14, 2013 · 2 min · Andrea Azzola

Turn Sentences into Slugs with JavaScript

This simple script converts a text string into a valid URL slug. It mixes jQuery with standard JavaScript, and can be easily adapted to pure JS. <!-- Example input element --> <input id="controlId" type="text" /> // Handles typing $(document).ready(function () { // If the Title is specified, avoid overwrite if ($('#controlId').val().length === 0) { $('#controlId').on('keypress', function () { $('#controlId').val(slugify($('#controlId').val().toLowerCase())); }); } }); // Replacements function slugify(text) { text = text.replace(/[^-a-zA-Z0-9,&\s]+/g, ''); text = text.replace(/-/g, '_'); text = text.replace(/\s/g, '-'); return text; } Note (2025): For modern projects you may also want to normalize accents (é → e), trim repeated separators, and prefer input event instead of keypress. This post keeps the original 2010 logic. ...

October 20, 2010 · 1 min · Andrea Azzola

POST data with JavaScript

Note (2025): This article dates back to 2009. The examples remain useful, though modern apps often prefer fetch()/XHR or form submissions driven by frameworks. The classic form‑build approach below still works universally. HTML 4.0 (W3C) defines how browsers submit forms: GET: append query string to the action URL (application/x-www-form-urlencoded). POST: send a request body to the action URL, using the content type indicated by enctype. Programmatic POST helper Add this in your page head: ...

July 7, 2009 · 1 min · Andrea Azzola

Submit di un Form tramite POST JavaScript

Secondo le specifiche HTML 4.0{:target="_blank" rel=“noopener”}: Se il metodo è GET, lo user agent prende il valore dell’azione, vi concatena un ? e il data set del form, mediante il content-type application/x-www-form-urlencoded. In questo caso i dati del form sono vincolati a codici ASCII. Se il metodo è POST, lo user agent effettua un submit HTTP POST utilizzando il valore dell’action e un messaggio creato in accordo con il content type specificato nell’attributo enctype. Vantaggi dei POST JavaScript Effettuare redirect diversi all’action Veicolare dati senza refresh della UI Evitare query string troppo lunghe e URL poco leggibili Esempio di submit POST Inserire il seguente codice nella sezione <head> della pagina HTML: ...

July 7, 2009 · 2 min · Andrea Azzola

JavaScript Auto-collapsing Notification Panel

Note (2025): This article was originally written in 2009. The example code (inline onclick, manual style.display changes) reflects practices of that era. Modern JavaScript uses event listeners, unobtrusive DOM manipulation, and CSS transitions. The post is kept here as an archive and for historical interest. In almost all web applications comes the moment when it becomes necessary to show some notifications to the user. A common practice in the past, even when elegance was unimportant and web pages didn’t show much dynamic behavior, was the use of the method alert() from JavaScript. ...

May 29, 2009 · 1 min · Andrea Azzola

Get and Set Radio Controls Checked Value with JavaScript

Suppose you have the following radio controls: <input name="Book" type="radio" value="asp" checked="true">ASP .NET</input> <input name="Book" type="radio" value="wcf">Windows Presentation Foundation</input> <input name="Book" type="radio" value="slg">Silverlight 2.0</input> How do you get the selected value via JavaScript? As you can see in the example, radio controls are declared with a name attribute (not an id). The browser treats each radio input as a different element, but the common name value groups them logically. The getElementsByName Method W3C Documentation — This method allows you to obtain a collection of controls from the DOM by specifying the common name value. ...

March 20, 2009 · 1 min · Andrea Azzola