Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q685 How can I prompt the user when they leave my page if they've amended a textarea without submitting the form?

You are here: irt.org | FAQ | JavaScript | Form | 11 | Q685 [ previous next ]

You'll not be able to stop someone from leaving the page in all cases. You can trap people selecting links on your page and then cancel them selectively, but not if the user uses the back of forward button, or selects an item from the history, or types in a url.

For links try:

<SCRIPT LANGUAGE="JavaScript"><!--
var changed = false;

function gothere(href) {
    if (changed) {
        if (confirm("Are you sure?"))
            window.location.href = href;
    }
    else
        window.location.href = href;
}
//--></SCRIPT>

<FORM onSubmit="changed=false">
<TEXTAREA onChange="changed=true;alert('changed');"></TEXTAREA>
<INPUT TYPE="SUBMIT"
</FORM>

<A HREF="javascript:gothere('http://www.irt.org')">http://www.irt.org</A>

Feedback on 'Q685 How can I prompt the user when they leave my page if they've amended a textarea without submitting the form?'

©2018 Martin Webb