Make a dialog confirming if a user wants to close page

my game doesn’t auto-save so I was trying to make it so that a popup shows when trying to unload/reload/close the page to prevent it from accidentally happening, in HTML this is accomplished by putting window.onbeforeunload = function () { return ""; } in a <script> tag, I tried putting this in a javascript event but it didn’t work, I achieved this in another game jumper but it uses the code in a script tag, but that is a version compiled to the web and then the script is added to index.html afterward, that’s more work than needed and i cant change the index.html file on so doing that is out of the picture anyways