title: Handle browser back button events in typescript
body: <p>i have this code from a article.</p>
  
  <pre><code>window.addEventListener('popstate', function (event) {
              // The popstate event is fired each time when the current history entry changes.
  
              var r = confirm("You pressed a Back button! Are you sure?!");
  
              if (r == true) {
                  // Call Back button programmatically as per user confirmation.
                  history.back();
                  // Uncomment below line to redirect to the previous page instead.
                  // window.location = document.referrer // Note: IE11 is not supporting this.
              } else {
                  // Stay on the current page.
                  history.pushState(null, null, window.location.pathname);
              }
  
              history.pushState(null, null, window.location.pathname);
  
          }, false);
  </code></pre>
  
  <p>where do i put this code in component in angular 2? is above code typescript compatible? I am not able to fire this event when i press back button as of now. is there any better way?</p>