Click here to Skip to main content
14,303,464 members
Rate this:
Please Sign up or sign in to vote.
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
Rate this:
Please Sign up or sign in to vote.

Solution 1

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
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

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[^]):
<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>
   
v2

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100