0

How can I notify user about unsaved plugin parameters? I have a plugin with a large number of settings and I would like to notify the user if he had made any changes to those settings but didn't save them yet. How could this be done?

2

The most commonly used approach is to hook the onbeforeunload event, like Stackoverflow itself does.

window.onbeforeunload = function(){ 
    // triggered when the user tries to navigate away / close the page
    return 'Your custom message';
}

This will ask the user whether they want to really close the page, along with your custom message.

See here or ask google for more info

However, if you want to be more user-friendly, you could store in localstorage while the user fills in the details, and keep your copy of the settings there.

With the second approach - or a combination of the two - you'd achieve a better result, more user friendly, but you'll definitely be working harder than the first approach, which simply requires one line of code.

0

Lets consider one input for example scenario. Suppose you have one input in your form named pluginname.

<input name="pluginname" id="pluginname" value="thisandthat" />

If you don't want to use localstorage (cause if may cause trouble on older browser). You can follow bellow trick.

On page load set one flag that will state us if any changes has been made to input with true/false.

jQuery(document).ready(function(){
    var changeflag = false,
        originalpluginname = '';
});

Bind focusin and focusout events for that element.

jQuery('#pluginname').focusin(function(){
    if(originalpluginname === '') 
        originalpluginname = this.value;
}).focusout(function(){
    if(originalpluginname !== this.value) 
        changeflag = true;
});

And then bind event to onbeforeunload to check if there any changes made by user. If use has made any change warn him.

$(window).on('beforeunload', function(){
    if(changeflag)
        return confirm('You have some unsaved changes, are you sure you want to leave before you save them?');
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.