How can the usage of browser alerts be restricted in a way that doesn’t limit the usage of this feature for positive UX?

Many of us have probably been trapped by a clever use of the browser alert feature by a spam website that we had the misfortune of being sent to. You try to exit and get another alert, which stops you from leaving the page. This is an important feature for UX in a way, because it allows web developers to save the user from accidentally exiting before work has been saved or where user input might be lost.

On the other hand, I think we could solve the issue of trapping via alert if, say, W3C specified that an alert could only be triggered within a certain time frequency.

Is there any case where such a restriction on web page alerts might damage UX? Or rather is there a different solution that might solve the problem in a way that doesn't limit the way this feature can be used for positive UX?

I've sometimes noticed an option to disable alerts by a page, but this is only present a fraction of the time. There's a way for the ignore functionality to be tricked / disabled it would seem, so I'm not sure that's a good solution.