0

I have employees who keep breaking things by opening my app in two tabs. I want to kill a tab if they open a new tab. Is this possible? window.close() will not work because my tab did not open itself. I was thinking of maybe writing a fork bomb, but that might crash something else. I am using Angular so I was thinking there must be some function I can call like the app.destory function?

Does anyone have any better ideas?

My app is ONLY run on Chrome and that will never change so a chrome specific solution is okay with me.

Samuel Thompson
  • 2,429
  • 4
  • 24
  • 34
  • 6
    better idea: write your code so that it works in more than one tab :P. – Heretic Monkey Apr 28 '20 at 19:53
  • @Monkey I dont like that idea.... it is hard.... all of my website is VERY VERY state dependant, and all the state is stored in localstorage. which is shareable between tabs – Samuel Thompson Apr 28 '20 at 19:57
  • You could encapsulate your app using https://www.electronjs.org/ – Mario Perez Apr 28 '20 at 20:07
  • @mario I have tried that, and my bosses got all up in a huff that you would have to install something. They only want a browser. – Samuel Thompson Apr 28 '20 at 20:17
  • 1
    thats a shame, but hey I found a similar question to yours here : https://stackoverflow.com/questions/23690666/check-if-my-website-is-open-in-another-tab Perhaps this will suit you, good luck. – Mario Perez Apr 28 '20 at 20:22

2 Answers2

1

You can access window object in Angular this way. Firsly we have to create a service. Since our Angular application isn’t only designed for running within your browser, but on mobiles platforms, the server or web workers where objects like window may not be available.

Therefore the suggested approach is to wrap such objects and inject them through the dependency injection mechanism.

import { Injectable } from '@angular/core';

function _window() : any {
   // return the global native browser window object
   return window;
}

@Injectable({
  providedIn: root
})
export class WindowRef {
   get nativeWindow() : any {
      return _window();
   }
}

And we can simply call that in our component like so:

import { WindowRef } from './WindowRef';

@Component({...})
class MyComponent {

    constructor(private winRef: WindowRef) {
        // getting the native window obj
        console.log('Native window obj', winRef.nativeWindow);
    }

}

Now create a function that handles the newtab action

function openInNewTab(url) {
  let newTab = this.winRef.open('https://stackoverflow.com/questions/61488800/kill-angular-in-chrome', '_blank');
  newTab.focus(); //=====> Focus new tab

  let currentTab = this.winRef.open("", "_self");
  currentTab.document.write("");
  currentTab.close();

}
Mr Khan
  • 2,139
  • 1
  • 7
  • 22
0

I found out there is a destroy function in angular: platformBrowserDynamic().destroy()

Just call that function and it will entirely unload angular. All CSS will stop working, and your HTML will freeze. It is not a bad option. I just freeze everything with a nice message saying to close the window.

Samuel Thompson
  • 2,429
  • 4
  • 24
  • 34