> For the complete documentation index, see [llms.txt](https://javascriptuse.gitbook.io/advanced/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://javascriptuse.gitbook.io/advanced/javascript/pwa-beforeinstallprompt-toplusgames.com-ok.md).

# PWA - beforeinstallprompt toplusgames.com (ok)

C:\xampp82\htdocs\html\index.html

```html
<html>
<head>
  <title>Test</title>
  <link rel="manifest" href="manifest.json">
</head>
<body>
  <p>
    If the <code>beforeinstallprompt</code> event fires, there will be a button displayed allowing
    you to use <code>prompt()</code> on the deferred event.
  </p>
  <button hidden>Show Prompt</button>
  <script>
    if ('serviceWorker' in navigator) {
      navigator.serviceWorker.register('sw.js');
      window.addEventListener('beforeinstallprompt', event => {
        event.preventDefault();
        var button = document.querySelector('button');
        button.removeAttribute('hidden');
        button.addEventListener('click', () => {
          event.prompt();
          button.setAttribute('disabled', true);
        });
      });
    }
  </script>
</body>
</html>
```

C:\xampp82\htdocs\html\manifest.json

```json
{
  "short_name": "PWA Test",
  "name": "PWA Test",
  "icons": [
    {
      "src": "https://raw.githubusercontent.com/jeffposnick/create-react-pwa/master/favicon.ico",
      "sizes": "192x192",
      "type": "image/png"
    }
  ],
  "start_url": "./",
  "display": "standalone"
}
```

C:\xampp82\htdocs\html\sw\.js

```javascript
self.addEventListener('install', () => self.skipWaiting());
self.addEventListener('activate', () => self.clients.claim());
```

## Áp dụng

<figure><img src="https://2494213435-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LYjAG13rAPdbArSfRyB%2Fuploads%2FAOBKL3hyv7oXSItLslDu%2Fimage.png?alt=media&amp;token=0006fbc1-c3be-4c21-b091-b696dff46472" alt=""><figcaption></figcaption></figure>

C:\xampp82\htdocs\testcom\static\script.js

```javascript
if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('https://test.com/static/js/sw.js');
  window.addEventListener('beforeinstallprompt', event => {
    event.preventDefault();
    var button = document.getElementById('installApp');
    button.removeAttribute('hidden');
    button.addEventListener('click', () => {
      event.prompt();
      button.setAttribute('disabled', true);
    });
  });
}
```

C:\xampp82\htdocs\testcom\static\js\sw\.js

```javascript
self.addEventListener('install', () => self.skipWaiting());
self.addEventListener('activate', () => self.clients.claim());
```

C:\xampp82\htdocs\testcom\wp-content\themes\theme-zoki\header.php

```php
<link rel="manifest" href="/media/manifest.json" />
```

## How do you prompt a user to install a [PWA](https://dev.to/mtee/how-do-you-prompt-a-user-to-install-a-pwa-56ef)?

[#100daysofcode](https://dev.to/t/100daysofcode)[#javscript](https://dev.to/t/javscript)

*My lesson of the day. The browser in use: chrome*

For a PWA to be installable, you need a registered service worker and a manifest file with the relevant fields.

You also need to attach a click event to an element in the HTML file. I added a button tag as the last element of my HTML file and set the display to none in CSS. *Note: The button element is empty.*

Next inline is the javascript code:<br>

```
let defferedPrompt;
const addbtn = document.querySelector('.btn');

window.addEventListener('beforeinstallprompt', event => {
    event.preventDefault();
    defferedPrompt = event
    addbtn.style.display = 'block';
});

addbtn.addEventListener('click', event => {
    defferedPrompt.prompt();

    defferedPrompt.userChoice.then(choice => {
        if(choice.outcome === 'accepted'){
            console.log('user accepted the prompt')
        }
        defferedPrompt = null;
    })
})
```

The code explained:

You listen for the `beforeinstallprompt` event and save the event to a variable. Then attach an event listener to your HTML element and listen for the click event. You also pass in a callback function to the event listener that calls `prompt()` on the saved event. Prompt displays a dialogue box. Using the User choice property you capture the user's action. User choice is a property that returns the users choice. If the users choice equates to accepted the installation begins else the dialog box is closed. You can only call `prompt()` on the deferred event once. Lastly, you set the deferred event to null.

*That's it for day 68*\
*Lets do this again tomorrow*
