-2

I am using a 3rd party js that shows alert's to the user, is there a way to catch the alert before it is displayed, and show something else instead? example:

function doOtherPeopleCode(){
   sdk.doStuff(); //catch all sdk alerts
}
Itsik Mauyhas
  • 3,824
  • 14
  • 69
  • 114

1 Answers1

1

Yes, you can simply replace the window.alert method with your custom version. Inside this replacement you can add some checks and conditions and then decide if you want to call the cached version of window.alert (which you store in a variable) or do something else.

Jonathan
  • 8,771
  • 4
  • 41
  • 78
  • We should have known this was a duplicate. I've deleted my answer, recommend doing the same. – T.J. Crowder Apr 25 '18 at 11:40
  • 1
    @T.J.Crowder I would not agree that it is an exact duplicate, since the duplicate talks about overriding alert and this one here asks about catching an alert before it happens. Overriding is just one way of doing it. – Phiter Apr 25 '18 at 11:41
  • @Phiter: There's no other vaguely-reasonable way to do it within the constraints the OP listed. The answers to the linked question answer this question. That's not even a close call by SO's duplicate rules. – T.J. Crowder Apr 25 '18 at 11:54
  • @T.J.Crowder As phiter said it is exact duplicate, the source for the alert is different but fill free to downvote.... – Itsik Mauyhas Apr 25 '18 at 12:01
  • BTW - is there a way to access the alert text I just overwrited? `this` inside shows the window. – Itsik Mauyhas Apr 25 '18 at 12:03
  • @itsik the text would be the first argument provided to the method – Jonathan Apr 25 '18 at 12:15