Skip to content

Pwa installation own button installation in JS + HTML + CSS

I know how to do a pwa integration and the browser proposes the installation with the message “Add — to the home screen”. My wish is as follows: I would like to set up on the integrated PWA website a personalized button l “Install mobile version” in HTML so that my users can install the progressive web app as needed, especially when they wish. They will not necessarily wait for the browser to offer them the installation. I have already done the PWA integration and the browser already offers the installation. Problem is, I don’t really know how to go about my permanent web app progressive install button in html + javascript that users will see on my website outside of what the browser offers.

Here is my service-worker.js and my index.html

var CACHE_NAME = 'PWA-installation';
var urlsToCache = [

self.addEventListener('install', function(event) {
  // Perform install steps
      .then(function(cache) {
        console.log('Opened cache');
        return cache.addAll(urlsToCache);

self.addEventListener('fetch', function(event) {
      .then(function(response) {
        // Cache hit - return response
        if (response) {
          return response;
        return fetch(event.request);
<!DOCTYPE html>
<html lang="en">

    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <!-- Web App Config -->
    <link rel="stylesheet" type="text/css" href="styles.css">
    <link rel="manifest" href="manifest.json">

<body translate="no">
    <header >
        <a href="#" id="install" class="icon-button" title="Install PWA" >
             Install PWA
    <!-- Footer -->
    <footer class="column">
    <!-- Scripts -->
    <script src="scripts/network.js"></script>
    <script src="scripts/ui.js"></script>
    <script src="scripts/theme.js" async></script>
    <script src="scripts/clipboard.js" async></script>



Your code looks fine, you only need to implement this part:

First you need to use this code to listen to the beforeinstallprompt event:

let deferredPrompt;

window.addEventListener('beforeinstallprompt', function (e) {

  // Prevent the mini-infobar from appearing on mobile

  // Stash the event so it can be triggered later.
  deferredPrompt = e;

Then you need to add the event listener to your button:

var buttonInstall = document.getElementById('install');

buttonInstall.addEventListener('click', function (e) {
  // Show the install prompt