How can we add the “add to home screen” button in our website using HTML and JavaScript only (no frameworks)?

To add an "Add to Home Screen" button to your website using HTML and JavaScript without using any frameworks, you can leverage the Web App Manifest and Service Worker. Here's a basic example:

  1. Create a Web App Manifest (manifest.json): Create a file named manifest.json in your project root with the following content:

     jsonCopy code{
       "name": "Your App Name",
       "short_name": "App",
       "description": "Description of your app",
       "start_url": "/",
       "display": "standalone",
       "background_color": "#ffffff",
       "theme_color": "#000000",
       "icons": [
         {
           "src": "/icon.png",
           "sizes": "192x192",
           "type": "image/png"
         }
       ]
     }
    

    Adjust the values according to your application. Ensure that you have an icon image (e.g., icon.png) in your project.

  2. Link the Manifest in your HTML: Add the following link tag in the <head> section of your HTML file:

     htmlCopy code<link rel="manifest" href="/manifest.json">
    
  3. Create a Service Worker (sw.js): Create a file named sw.js in your project root with the following content:

     javascriptCopy codeself.addEventListener('install', (event) => {
       event.waitUntil(
         caches.open('your-app-cache').then((cache) => {
           return cache.addAll(['/index.html', '/manifest.json', '/icon.png']);
         })
       );
     });
    
     self.addEventListener('fetch', (event) => {
       event.respondWith(
         caches.match(event.request).then((response) => {
           return response || fetch(event.request);
         })
       );
     });
    

    This service worker will cache the essential files for offline use.

  4. Register the Service Worker in your HTML: Add the following script tag at the end of your HTML file, just before the closing </body> tag:

     htmlCopy code<script>
       if ('serviceWorker' in navigator) {
         navigator.serviceWorker.register('/sw.js')
           .then((registration) => {
             console.log('Service Worker registered with scope:', registration.scope);
           })
           .catch((error) => {
             console.error('Service Worker registration failed:', error);
           });
       }
     </script>
    
  5. Add a Button in your HTML: Place a button wherever you want on your page with the following code:

     htmlCopy code<button onclick="promptAddToHomeScreen()">Add to Home Screen</button>
    
  6. Add JavaScript Function: Add the following JavaScript function to prompt the user to add the app to the home screen:

     htmlCopy code<script>
       function promptAddToHomeScreen() {
         if (window.matchMedia('(display-mode: standalone)').matches) {
           console.log('This is running as standalone.');
         } else if (navigator.standalone || window.matchMedia('(display-mode: standalone)').matches) {
           console.log('Running in standalone');
         } else {
           alert('To add this web app to your home screen, tap "Add to Home Screen"');
         }
       }
     </script>
    

    Customize the alert message as needed.

Remember to adjust paths, names, and other details according to your project structure and requirements.