Click here to Skip to main content
15,886,833 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

I have a situation where I need to show a confirmation message to the users of my website when users of my website leaves my website or closes the window.

But also I want to know on which website user has shifted (if possible) from my website.

Is it possible in JavaScript or any other way.

Currently I have found following code :
var currentWebSite;
window.onload = SetURL;
window.onunload = unloadPage;

function unloadPage()
{
    alert(window.location);
    alert("unload event detected!");
}
function SetURL()
{
    currentWebSite = window.location.host;
    alert(window.location.host);
}
Posted
Updated 6-Jan-12 17:58pm
v3

Just think on it a bit: suppose your user already "shifted" to some Web site. What does it mean? It simply means that the user sent some HTTP request to other site, received HTTP response, rendered the page, elaborated some scripts, etc. Too late — at this moment, current page has nothing to do with your site.

Of course you can prevent navigation out of your site. Theoretically speaking, a Web browser could trigger some event before navigating out of your site. Unfortunately, the event onbeforeunload is proprietary to Microsoft, see http://msdn.microsoft.com/en-us/library/ms536907%28v=vs.85%29.aspx[^], so you cannot reliably use it as it may not work with different browsers. I did not even try, please see http://discuss.joelonsoftware.com/default.asp?design.4.357494.19[^].

Moreover, even the window.onunload event processing is different in different browsers. I just tested it with IE under Microsoft JavaScript debugger — it can accept one parameter with a lot of even information. Unfortunately, this is not part of the standard and not documented.

—SA
 
Share this answer
 
Hi SA,

Thanks for the information. What I have found is we can use onbeforeunload event supported in IE (for suerly), also tested in Firefox7.0, and Crome. But still this is not supported in many browsers like Opera.

My solution is (Original post is : http://www.4guysfromrolla.com/demos/OnBeforeUnloadDemo2.htm[^]):
ASP.NET
<head  runat="server">
    <title>:Demo : Message on page closure</title>
    <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
    <script type="text/javascript" src="js/jquery-1.6.1.min.js"></script>
    <script type="text/javascript">
    
    window.localLinkClicked = false;
     var needToConfirm = true;
     
      window.onbeforeunload = confirmExit;
      
      function confirmExit()
      {
        if (window.localLinkClicked) {
            needToConfirm = false
        } else {
            needToConfirm = true;
        }
        if (needToConfirm)
        {
            needToConfirm=false;
            return "You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?";
        }
        
        //window.localLinkClicked = false;
      }
      

    $("a").live("click", function() {
        var url = $(this).attr("href");
        
        url=url.toLowerCase();
        
        if(url.indexOf('http') >=0 && url.indexOf('localhost:1177') < 0)
            window.localLinkClicked = false;
        else
            window.localLinkClicked = true;
    });

    
    </script>
</head>
<body>
    <form id="form1"  runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <div>
            <asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="~/Default2.aspx">Go to Page 2</asp:LinkButton>
            <asp:LinkButton ID="LinkButton2" runat="server" PostBackUrl="http://www.google.co.in">Go to Google</asp:LinkButton>
            <br />
            <br />
            To postback click the button below:<br />
            
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" OnClientClick="window.localLinkClicked = true;" Text="Postback" />
            <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" OnClientClick="window.localLinkClicked = true;" Text="Server side Redirect to Page 2" /><br />
            <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" OnClientClick="window.localLinkClicked = false;" Text="Server side Redirect to Google" /><br />
            <asp:Label ID="Label1" runat="server"></asp:Label></div>
    </form>
</body>
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900