Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript window.onsave event

Is there a way to detect/intercept when the user tries to save the page? This would allow me to embed any external files properly and provide the user with a fully-functional offline app.

The other solution is embedding those resources from the beginning, but it consumes too many resources and and takes away some of the dynamic capability.

Any alternative that does not require any external libraries (including jQuery, I respect it, but it loads too much for this project) is acceptable.

like image 980
Zyox Avatar asked Dec 05 '25 13:12

Zyox


1 Answers

There is no window.onsave event that I could find. However, you COULD listen for the ctrl+s keystroke which is easily intercepted.

var isCtrl = false;
document.onkeyup=function(e){
    if(e.keyCode == 17) isCtrl=false;
}

document.onkeydown=function(e){
    if(e.keyCode == 17) isCtrl=true;
    if(e.keyCode == 83 && isCtrl == true) {
        //run code for CTRL+S -- ie, save!
        return false;
    }
}

Code courtesy of: How do I capture a CTRL-S without jQuery or any other library?

like image 111
David L Avatar answered Dec 09 '25 16:12

David L



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!