favico.js - Documentation

Introduction

What is favico.js?

Favico.js is a lightweight JavaScript library that allows you to dynamically update your website’s favicon. Instead of being limited to a single static favicon image, favico.js enables you to change the favicon’s appearance based on various events or conditions on your website. This can include displaying unread notification counts, showing different icons for different pages, or even creating animated favicons. It achieves this by manipulating the <link> tag that references the favicon, cleverly handling browser-specific quirks and ensuring compatibility across a wide range of browsers.

Why use favico.js?

Using favico.js offers several key advantages:

Key Features

Browser Compatibility

Favico.js strives for broad browser compatibility, supporting modern versions of the following:

While older browsers may have limited or no support for dynamic favicon updates, favico.js gracefully handles these situations, ensuring that your website remains functional even in less-supported environments. For optimal results, targeting modern browsers is recommended. Specific compatibility details for older browser versions should be checked in the library’s documentation and release notes.

Getting Started

Installation

Favico.js can be installed via several methods:

<script src="https://cdn.jsdelivr.net/npm/favico.js@0.3.10/dist/favico.js"></script>  <!-- Replace 0.3.10 with the latest version number -->

Remember to replace 0.3.10 with the latest version number available on jsDelivr or your preferred CDN. Check the official favico.js repository for the most up-to-date version.

npm install favico.js

This will download the library to your project’s node_modules directory. You’ll then need to include it in your HTML file (see the “Including favico.js in your project” section below).

Basic Usage

After including favico.js in your project, you can use its API to easily manage your favicon. The core functionality revolves around creating a favico object and using its methods to update the favicon. The most common methods are badge, reset, and update. badge adds a numerical badge to the favicon, reset removes any badges or customizations, and update allows for completely replacing the favicon image.

Including favico.js in your project

Regardless of your installation method, you’ll need to include favico.js in your HTML file using a <script> tag. If you used npm, the path will be relative to your project’s structure. For example:

Using CDN: (Already shown in the Installation section)

Using npm: Assuming you’ve placed your favicon images in a public/images folder:

<script src="./node_modules/favico.js/dist/favico.js"></script>

Place this <script> tag ideally just before the closing </body> tag of your HTML, to ensure the DOM is fully loaded before favico.js attempts to manipulate the favicon.

First Example

This example demonstrates adding a badge to your favicon:

<!DOCTYPE html>
<html>
<head>
  <title>Favico.js Example</title>
  <script src="https://cdn.jsdelivr.net/npm/favico.js@0.3.10/dist/favico.js"></script> </head>
<body>

  <script>
    const favicon = new Favico({
      animation: 'none' //Optional: Set animation type
    });

    // Add a badge with the number 5
    favicon.badge(5);


    // after 3 seconds, reset the favicon to default
    setTimeout(()=>{
        favicon.reset();
    }, 3000)


  </script>

</body>
</html>

This code first includes favico.js from a CDN. Then, it creates a Favico object. Finally, it uses the badge() method to display a badge with the number 5 on the favicon. Remember to replace the CDN link with your preferred method of including the library. Also, ensure you have a favicon.ico file in your project’s root directory. You might need to adjust paths depending on your project setup. The animation option is used to prevent unwanted animations if you don’t need them.

API Reference

favico().badge(number, options)

Adds a numerical badge to the favicon.

Example:

favicon.badge(5); // Adds a badge with the number 5
favicon.badge(0); // Removes the badge
favicon.badge(12, { color: 'red' }); //Adds a red badge with number 12

favico().reset()

Resets the favicon to its default state. Removes any badges or custom images applied previously.

Example:

favicon.reset();

favico().update(image [, options])

Updates the favicon with a new image.

Example (using image path):

favicon.update('/path/to/new-favicon.png');

Example (using image object - for more control):

favicon.update({
    src: '/path/to/new-favicon.png',
    type: 'image/png' //Optional
});

favico().show()

Makes the favicon visible if it was previously hidden.

Example:

favicon.show();

favico().hide()

Hides the favicon. Useful for temporarily disabling the favicon or showing a different element instead.

Example:

favicon.hide();

Options and parameters

Several options can be used with badge() and update() methods:

Customizing the favicon

Favico.js allows for significant customization. You can control the appearance of the badge (color, position) and even replace the entire favicon image with a different one using the update() method. For more advanced customization you might consider manipulating the favicon image itself or using advanced animation techniques and options provided by the library. Refer to the library’s documentation for the most complete and up-to-date list of options and examples of advanced customization.

Advanced Usage

Using with frameworks (React, Angular, Vue)

Favico.js is framework-agnostic and can be easily integrated into popular JavaScript frameworks like React, Angular, and Vue. The core integration remains the same: include the library and use its API. However, the way you manage the favicon state and trigger updates will depend on your chosen framework’s state management and lifecycle methods.

Example (Conceptual React):

import React, { useState, useEffect } from 'react';
import Favico from 'favico.js'; //Import after installation using npm

function MyComponent() {
  const [notificationCount, setNotificationCount] = useState(0);
  const favicon = new Favico();

  useEffect(() => {
    favicon.badge(notificationCount);
    //Cleanup function for component unmount:
    return () => favicon.reset();
  }, [notificationCount, favicon]);

  const handleNewNotification = () => {
    setNotificationCount(notificationCount + 1);
  };

  return (
    <div>
      <button onClick={handleNewNotification}>New Notification</button>
    </div>
  );
}

This React example demonstrates a simple counter that updates the favicon badge. Remember to adapt this based on your specific framework’s patterns for handling state and side effects. For Angular and Vue, similar principles apply; you’ll use the framework’s lifecycle hooks and state management systems to integrate favico.js effectively.

Handling multiple favicons

While favico.js primarily manages a single favicon, you can achieve the effect of multiple favicons by strategically using the update() method. For instance, you could have different favicon images for different states and switch between them based on your application’s logic. You could create an array of favicon URLs and update dynamically.

const favicon = new Favico();
const faviconUrls = ['/path/to/favicon1.png', '/path/to/favicon2.png'];
let currentFaviconIndex = 0;

function changeFavicon(index){
    if(index >= faviconUrls.length) {return;}
    favicon.update(faviconUrls[index]);
    currentFaviconIndex = index;
}

// ...later in your code...
changeFavicon(1); //Use the second favicon image

Dynamically updating favicons

Dynamic favicon updates are a core feature of favico.js. The examples above already demonstrated this by updating the badge or changing favicon images based on changing application state. Use the badge() and update() methods within event handlers, timers, or other parts of your application’s logic to trigger favicon changes. For instance, you might update the badge when new notifications arrive via WebSockets or AJAX requests. Make sure you handle potential race conditions carefully if multiple updates are occurring simultaneously.

Error Handling

Favico.js generally handles browser inconsistencies gracefully. However, there are potential points of failure, particularly when working with invalid image URLs or unsupported browser features. For robust error handling, you could:

By addressing these points, you can create a more robust and user-friendly application that utilizes the full power of favico.js.

Examples

Simple counter

This example shows a simple counter that updates a numerical badge on the favicon:

<!DOCTYPE html>
<html>
<head>
<title>Favico.js Counter Example</title>
<script src="https://cdn.jsdelivr.net/npm/favico.js@0.3.10/dist/favico.js"></script>
</head>
<body>

<button id="increment">Increment</button>
<p id="count">0</p>

<script>
  const countElement = document.getElementById('count');
  const incrementButton = document.getElementById('increment');
  let count = 0;
  const favicon = new Favico({ animation: 'none' });

  incrementButton.addEventListener('click', () => {
    count++;
    countElement.textContent = count;
    favicon.badge(count);
  });
</script>

</body>
</html>

This code creates a button that increments a counter displayed on the page and updates the favicon badge accordingly. Remember to have a favicon.ico in your project’s root directory.

Notification badge

This example simulates receiving notifications and updates a badge accordingly:

<!DOCTYPE html>
<html>
<head>
<title>Favico.js Notification Example</title>
<script src="https://cdn.jsdelivr.net/npm/favico.js@0.3.10/dist/favico.js"></script>
</head>
<body>

<button id="addNotification">Add Notification</button>

<script>
  const addNotificationButton = document.getElementById('addNotification');
  let notificationCount = 0;
  const favicon = new Favico({ animation: 'pop' });

  addNotificationButton.addEventListener('click', () => {
    notificationCount++;
    favicon.badge(notificationCount);
  });
</script>

</body>
</html>

This uses the ‘pop’ animation for the badge. Clicking the button simulates a new notification.

Progress bar

This example is more complex and requires additional logic to manage the progress:

<!DOCTYPE html>
<html>
<head>
<title>Favico.js Progress Bar Example</title>
<script src="https://cdn.jsdelivr.net/npm/favico.js@0.3.10/dist/favico.js"></script>
</head>
<body>

<div id="progressBar"></div>

<script>
  const progressBar = document.getElementById('progressBar');
  let progress = 0;
  const favicon = new Favico();

  const interval = setInterval(() => {
    progress++;
    if (progress > 100) {
      clearInterval(interval);
      favicon.reset();
    } else {
      progressBar.style.width = `${progress}%`;
      favicon.badge(progress);
    }
  }, 100);
</script>

</body>
</html>

This example simulates a progress bar using a div element and updates the favicon badge to reflect the progress.

Custom image favicon

This example updates the favicon with a custom image:

<!DOCTYPE html>
<html>
<head>
<title>Favico.js Custom Image Example</title>
<script src="https://cdn.jsdelivr.net/npm/favico.js@0.3.10/dist/favico.js"></script>
</head>
<body>

<button id="changeFavicon">Change Favicon</button>

<script>
  const changeFaviconButton = document.getElementById('changeFavicon');
  const favicon = new Favico();
  const customFavicon = '/path/to/your/custom-favicon.png'; // Replace with your image path

  changeFaviconButton.addEventListener('click', () => {
    favicon.update(customFavicon);
  });
</script>

</body>
</html>

Remember to replace /path/to/your/custom-favicon.png with the actual path to your custom favicon image.

Complex example integrating multiple features

This example combines multiple features: a counter, a custom favicon, and animation:

<!DOCTYPE html>
<html>
<head>
<title>Favico.js Complex Example</title>
<script src="https://cdn.jsdelivr.net/npm/favico.js@0.3.10/dist/favico.js"></script>
</head>
<body>

<button id="increment">Increment</button>
<button id="changeFavicon">Change Favicon</button>

<script>
  let count = 0;
  const favicon = new Favico({animation: 'pulse'});
  const incrementButton = document.getElementById('increment');
  const changeFaviconButton = document.getElementById('changeFavicon');
  const customFavicon = '/path/to/your/custom-favicon.png'; //Replace with your path

  incrementButton.addEventListener('click', () => {
    count++;
    favicon.badge(count);
  });

  changeFaviconButton.addEventListener('click', () => {
    favicon.update(customFavicon);
  });
</script>

</body>
</html>

This combines incrementing a counter with a badge, and changing the favicon image using a button. Remember to replace the placeholder image path. Adjust the animation as needed. These examples illustrate the basic usage and some advanced features; more sophisticated applications may require more intricate logic and state management. Always refer to the library’s documentation for the latest features and detailed explanations.

Troubleshooting

Common issues and solutions

Debugging tips

Troubleshooting browser-specific issues

While favico.js attempts to handle browser differences, some quirks may still arise. Here are some browser-specific considerations:

Remember to always consult the official favico.js documentation and release notes for the most current information on browser compatibility and troubleshooting.

Contributing

Contributing to the project

We welcome contributions to favico.js! If you find a bug, have a feature request, or want to improve the code, please follow these steps:

  1. Fork the repository: Create your own fork of the favico.js repository on GitHub.

  2. Create a new branch: Create a new branch for your changes. Use a descriptive name that reflects the purpose of your contribution (e.g., fix-bug-123, add-feature-xyz).

  3. Make your changes: Make your code changes, following the code style guide (see below). Ensure your changes are well-documented and tested.

  4. Test your changes: Thoroughly test your changes to ensure they work correctly and don’t introduce new bugs. See the “Testing and CI/CD process” section for details.

  5. Commit your changes: Commit your changes with clear and concise commit messages that explain what you’ve done and why.

  6. Push your branch: Push your branch to your forked repository on GitHub.

  7. Create a pull request: Create a pull request from your branch to the main main or master branch of the original favico.js repository. Provide a clear description of your changes in the pull request, including any relevant context or background information.

  8. Address feedback: Address any feedback or suggestions from the maintainers of the favico.js project. Make necessary revisions and push them to your branch.

Code style guide

Favico.js follows a consistent code style to maintain readability and maintainability. The specific style guide may be documented in a separate file within the repository (e.g., .editorconfig, .eslintrc). Adherence to this style guide is essential for all contributions. Generally, this will involve:

Testing and CI/CD process

Favico.js utilizes automated testing to ensure code quality and prevent regressions. The specific testing framework and CI/CD (Continuous Integration/Continuous Delivery) pipeline may be detailed in the repository’s documentation (e.g., a README.md file). Generally, this process might involve:

Before submitting a pull request, ensure your changes pass all tests in the CI/CD pipeline. The repository’s documentation will provide more specific instructions on how to run the tests locally.

License

License information

Favico.js is typically released under an open-source license, allowing for free use, modification, and distribution. However, the specific license (e.g., MIT, GPL, Apache 2.0) will be clearly stated within the project’s repository, usually in a file named LICENSE or LICENSE.txt. Always refer to that file for the exact terms and conditions governing the use of favico.js. The license file will outline the permitted uses, restrictions, and disclaimers associated with the software. It’s crucial to understand and comply with the terms of the license before using, modifying, or distributing favico.js.