diff --git a/js/owl.js b/js/owl.js
index 3191c0d216dd0cec84cc2a0adbcf2ab0429ead76..2b5c94374d2d98f999a75304ec3ac02ae638c100 100644
--- a/js/owl.js
+++ b/js/owl.js
@@ -70,13 +70,19 @@ exports.add_ctrl_enter = function (f) {
 	}
 }
 
+let do_unload_check = true;
+
 exports.add_unload_check = function (f) {
 	if (f) {
 		const orig_contents = f.value;
 		window.addEventListener('beforeunload', (event) => {
-			if (f.value != orig_contents) {
+			if (do_unload_check && f.value != orig_contents) {
 				event.preventDefault();
 			}
 		});
 	}
 }
+
+exports.cancel_unload_check = function () {
+	do_unload_check = false;
+}
diff --git a/owl/post.py b/owl/post.py
index 291891447952fad8c5fa890341cbdea76f6cc6ce..347a55fd51a3f3defb08e7930bbbc0bd1e016ea9 100644
--- a/owl/post.py
+++ b/owl/post.py
@@ -320,6 +320,12 @@ def edit_post(sident: str, cident: str, pid: int):
     else:
         must_copy = deadline_passed or has_followup(post)
 
+    if g.is_grader and post.target_uid >= 0:
+        student_uid = post.target_uid
+    else:
+        student_uid = None
+    return_to = url_for('topic_index', sident=g.course.semester.ident, cident=g.course.ident, tident=g.topic.ident, student_uid=student_uid)
+
     if request.method == 'POST':
         form = EditPostForm()
         if not form.validate_on_submit():
@@ -329,12 +335,6 @@ def edit_post(sident: str, cident: str, pid: int):
                 flash('Post not submitted, see below for errors.', 'error')
             return render_template('edit-post.html', form=form, must_copy=must_copy, deadline_passed=deadline_passed)
 
-        if g.is_grader and post.target_uid >= 0:
-            student_uid = post.target_uid
-        else:
-            student_uid = None
-        return_to = url_for('topic_index', sident=g.course.semester.ident, cident=g.course.ident, tident=g.topic.ident, student_uid=student_uid)
-
         if form.delete.data:
             if g.is_teacher:
                 app.logger.info(f"Deleted post: pid={pid} tid={post.tid} author_uid={post.author_uid} target_uid={post.target_uid} by_uid={g.uid} public={g.topic.public}")
@@ -400,7 +400,11 @@ def edit_post(sident: str, cident: str, pid: int):
         return redirect(return_to)
 
     form = EditPostForm(obj=post)
-    return render_template('edit-post.html', form=form, must_copy=must_copy, deadline_passed=deadline_passed)
+    return render_template('edit-post.html',
+                           form=form,
+                           must_copy=must_copy,
+                           deadline_passed=deadline_passed,
+                           return_to=return_to)
 
 
 # Serving files
diff --git a/owl/templates/edit-post.html b/owl/templates/edit-post.html
index 664bfbc71d5341c8874b1132d06dbddaaf09a7cc..52c79237b84f5d79414f0a8645c5e6499942ec3a 100644
--- a/owl/templates/edit-post.html
+++ b/owl/templates/edit-post.html
@@ -12,7 +12,7 @@
 		{% endif %}
 	{% endif %}
 
-	<form id=postform method="POST" action="">
+	<form id=postform method="POST" action="" onsubmit='window.owl.cancel_unload_check()'>
 		{{ form.csrf_token }}
 		{% if form.errors['comment'] %}
 			<p class=error>{{ form.errors['comment'][0] }}
@@ -32,11 +32,13 @@
 			<div id=preview>
 			</div>
 		</div>
-		<p>{{ form.submit_button(class='ok', accesskey='s') }}
+		<p class='inline-buttons'>{{ form.submit_button(class='ok', accesskey='s') }}
 		{% if not must_copy %}
 			{{ form.silent_submit(class='ok', title="Submit, but do not send notifications.") }}
 		{% endif %}
-		<button type=button accesskey='p' onclick='javascript:window.owl.preview()'>Preview</button>
+			<button type=button accesskey='p' onclick='javascript:window.owl.preview()'>Preview</button>
+			<a class=button href="{{ return_to }}" onclick='window.owl.cancel_unload_check()'>Cancel</a>
+		</span>
 		{% if not must_copy %}
 			<p>{{ form.delete(class='danger') }}
 		{% endif %}
diff --git a/owl/templates/topic.html b/owl/templates/topic.html
index d207c07d1bbcaefc94c3a562ce27aba13b668542..d5975a4c42f409fe95a9e342da0aa7b756e21486 100644
--- a/owl/templates/topic.html
+++ b/owl/templates/topic.html
@@ -129,7 +129,7 @@
 	</div>
 
 	<h3>New post <span id=syntaxhelp>(You can use <a href='https://www.markdownguide.org/cheat-sheet/'>Markdown</a> with <a href='https://katex.org/docs/supported.html'>KaTeX math</a> here)</span></h3>
-	<form id=postform method="POST" action="" enctype="multipart/form-data">
+	<form id=postform method="POST" action="" enctype="multipart/form-data" onsubmit='window.owl.cancel_unload_check()'>
 		{{ form.csrf_token }}
 		{% if form.errors['comment'] %}
 			<p class=error>{{ form.errors['comment'][0] }}