Category: JavaScript

  • JavaScript – Remove a specific element from an array

    It’s quite often that I have an array of elements and when a certain condition happens that I need to remove a particular element from my JavaScript array. Let’s explore how indexOf and splice accomplish this.

    (more…)

  • Difference between location.href and location.replace

    It’s quite common to redirect a user visiting your webpage with javascript location href, more specifically when they perform an action, such as: clicking a button. When the user completes the action and you want to redirect the user to somewhere else, you have two common choices: window.location vs window.location.href
    (more…)

  • Retrieving query string variables with JavaScript

    This is not something I use everyday. Seems more like a once a year thing. Last time I needed to extract query string variables, I used the old school approach with a regular expression. Now that I need to do this again, I can use the new school approach: URLSearchParams.

    The URLSearchParams (at the time of writing) is not yet standardized in the W3C; however, most modern browsers recognize it. I’ve also got a version of this for how-to Access query string parameters with Express and Node.js.

    (more…)

  • Zero Padding a Number with JavaScript

    This morning I had to format a date using JavaScript as follows: yyyymmdd requiring both the month and day to include a leading zero when the month or day was less than 10. This solution using slice multiple times.

    (more…)

  • Convert the (un)check all to a #KnockoutJS Component

    You’ve created a nice feature leveraging Knockout.js and now you want to re-use this feature on another page, another site, or in a slightly different fashion. This example will extend the previous (Un)Check All using Knockout.js and make it easily re-usable.

    A Knockout component can be created using a mixture of HTML (with data bindings) and a Knockout ViewModel. By altering the previous check all example and making it slightly more re-usable, it can be easily added to other pages with minimal effort. If you are not already familiar with the example being extended, please take a minute and give the Knockout js tutorial a quick read with the link provided above.

    Once the component is created, it can be included on any page with this simple HTML:

    <checkall params=”items: items, selectedItems: selectedItems”></checkall>

    (more…)