Prevent iframe breakout

20.Aug.2010

Prevent iframe breakout with simple onbeforeunload function. Define function on this event in order to prevent script inside of an iframe leaving it. This code asks user if he want’s to navigate away from your page. It is not the most elegant solution, but it is the one to prevent other script from iframe breakout – and it is the only one working in all browsers. So, use following snippet in order to prevent iframe breakout.

1
2
3
4
5
6
7
8
9
10
11
12
<scipt>
window.onbeforeunload = function (e) {
	var message = 'Are you sure you want to leave this page?';
	if (typeof e == 'undefined') {
		e = window.event;
	}
	if (e) {
		e.returnValue = message;
	}
	return message;
}
</script>