Click here to Skip to main content
11,495,758 members (63,658 online)
Click here to Skip to main content

Quick and dirty way to ask/warn user when navigating off the page.

, 20 Feb 2012 CPOL 6K 4
Rate this:
Please Sign up or sign in to vote.
I want to warn user before navigating off the current page that one could lose unsaved data etc.
It may seam trivial, but this was my first beginner's roadblock when shifting from desktop-based to web-based solutions and after some time has passed I felt obliged to share my solution and help some wandering beginner.

You can hook up your questioning logic on onbeforeunload event.
In your page's markup you should add following code:
<script language="JavaScript" type="text/javascript">
   window.onbeforeunload = confirmExit;
   function confirmExit() {
       return "Ask the user whether he/she really wants na navigate off the page.";
This will warn user every time.

There is however one major issue - post back, then code shown above becomes major annoyance.
For instance, you don't want to have popup warning when selecting new value from filtering drop down list or when clicking on submitting button...

Easy way to avoid such behavior is to add following code in markup on every control responsible for post back.

window.onbeforeunload = null;
It will stop firing onbeforeunload event and code mentioned above won't execute.
No popup on awkward place Smile | :) .

for instance
<asp:DropDownList ID="ddlTip" runat="server" DataSourceID="tipSqlDataSource" DataTextField="Naziv" DataValueField="TipArtiklID" AutoPostBack="True" CssClass="dropDownList" onchange="window.onbeforeunload=null">
<asp:Calendar ID="calendar" runat="server" onclick="window.onbeforeunload=null">

Some controls (such as buttons), have OnClientClick property where you can easily add desired javascript snippet in code behind as well.
protected void Page_Load(object sender, EventArgs e)
        btnOK.OnClientClick = "window.onbeforeunload=null";

I hope this helps.
Please warn me for grammar and spelling mistakes.
And please if you know something better, share it!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Oshtri Deka
Software Developer
Croatia Croatia
Software engineer.

C/C++ during college, .Net and C# first as a hobby, now it is a full-time job; SQL follows me wherever I go.

ASP.Net, JavaScript, HTML they were necessity, but slowly become my bread and butter.
Hopefully I'll:
1. master F#
2. find practical use for my Python knowledge
3. return to C

I like to think I am mature intermediate programmer Smile | :) .

Comments and Discussions

GeneralReason for my vote of 5 Nice article Pin
rk_prabakar21-Feb-12 18:47
memberrk_prabakar21-Feb-12 18:47 
GeneralRe: Thank you! Pin
Oshtri Deka21-Feb-12 22:19
memberOshtri Deka21-Feb-12 22:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 20 Feb 2012
Article Copyright 2012 by Oshtri Deka
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid