Solved

on browser close

Posted on 2013-01-31
13
277 Views
Last Modified: 2013-02-04
Hey guys i like to know is it possiable to sent an email when the user choose to close the browser

i have this js on my asp c# page

    <script type="text/javascript">
        var hook = true;
        window.onbeforeunload = function() 
        {
            if (hook) 
            {
                var x = document.getElementById('<%=lblArea.ClientID%>').innerText;
                return 'You have not verified your Bed and Breakfast in '+ x +', are you sure you wish to navigate away from this page? email sent to close page without verify:';
            }
        }
        function unhook() 
        {
            hook=false;
        }
    </script>

now if they click leave the page it must execute c# code to send them an email

Open in new window

0
Comment
Question by:JCWEBHOST
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
13 Comments
 
LVL 5

Expert Comment

by:indrajitmahajan
ID: 38838763
YEs.. but you will have to use the java scipt for the same.
 function doSomething(e) {
            var posx = 0;
            var posy = 0;
            if (!e) var e = window.event;
            if (e.pageX || e.pageY) {
                posx = e.pageX;
                posy = e.pageY;
            }
            else if (e.clientX || e.clientY) {
                posx = e.clientX + document.body.scrollLeft
                            + document.documentElement.scrollLeft;
                posy = e.clientY + document.body.scrollTop
                            + document.documentElement.scrollTop;
            }
            if (posy < 0) {
                    window.open('../Forms/LogOut.aspx', '_self');
                    return true;
            }
        }

        document.onkeydown = keydown;
        function keydown(evt) {
            if (!evt) evt = event;
            if (evt.altKey && evt.keyCode == 115) { //CTRL+ALT+F4
                window.open("../Forms/LogOut.aspx", '_self');
            }
        }

Now you can write your code in Logout.aspx to send an email
0
 

Author Comment

by:JCWEBHOST
ID: 38838872
how do i place  function doSomething in my code below?

    <script type="text/javascript">
        var hook = true;
        window.onbeforeunload = function() 
        {
            if (hook) 
            {
                var x = document.getElementById('<%=lblArea.ClientID%>').innerText;
                return 'You have not verified your Bed and Breakfast in '+ x +', are you sure you wish to navigate away from this page? email sent to close page without verify:';
            }
        }
        function unhook() 
        {
            hook=false;
        }
    </script>

Open in new window

0
 
LVL 5

Expert Comment

by:indrajitmahajan
ID: 38838911
Hi...

Please put above code in before </script>

also put below code:
<body  onunload="doSomething(event);">
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:JCWEBHOST
ID: 38839093
like this?

   <script type="text/javascript">
        function doSomething(e) 
        {
            var posx = 0;
            var posy = 0;
            if (!e) var e = window.event;
            if (e.pageX || e.pageY) 
            {
                posx = e.pageX;
                posy = e.pageY;
            }
            else if (e.clientX || e.clientY) 
            {
                posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
                posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
            }
            if (posy < 0) 
            {
                    window.open('../emails.aspx', '_self');
                    return true;
            }
        }

        document.onkeydown = keydown;
        function keydown(evt) 
        {
            if (!evt) evt = event;
            if (evt.altKey && evt.keyCode == 115) 
            { 
                window.open("../emails.aspx", '_self');
            }
        }
    </script>

    <script type="text/javascript">
        var hook = true;
        window.onbeforeunload = function() 
        {
            if (hook) 
            {
                var x = document.getElementById('<%=lblArea.ClientID%>').innerText;
                return 'You have not verified your Bed and Breakfast in '+ x +', are you sure you wish to navigate away from this page? email sent to close page without verify:';
            }
        }
        function unhook() 
        {
            hook=false;
        }
    </script>

Open in new window

0
 

Author Comment

by:JCWEBHOST
ID: 38839101
it not working :(
0
 
LVL 5

Accepted Solution

by:
indrajitmahajan earned 500 total points
ID: 38839123
Do like this.....

<script type="text/javascript">
        function doSomething(e)
        {
            var posx = 0;
            var posy = 0;
            if (!e) var e = window.event;
            if (e.pageX || e.pageY)
            {
                posx = e.pageX;
                posy = e.pageY;
            }
            else if (e.clientX || e.clientY)
            {
                posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
                posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
            }
            if (posy < 0)
            {
                    window.open('../emails.aspx', '_self');
                    return true;
            }
        }

        document.onkeydown = keydown;
        function keydown(evt)
        {
            if (!evt) evt = event;
            if (evt.altKey && evt.keyCode == 115)
            {
                window.open("../emails.aspx", '_self');
            }
        }

        var hook = true;
        window.onbeforeunload = function()
        {
            if (hook)
            {
                var x = document.getElementById('<%=lblArea.ClientID%>').innerText;
                return 'You have not verified your Bed and Breakfast in '+ x +', are you sure you wish to navigate away from this page? email sent to close page without verify:';
            }
        }
        function unhook()
        {
            hook=false;
        }
    </script>

Also in body tag add:
<body  onunload="doSomething(event);">

It should work..
0
 

Author Comment

by:JCWEBHOST
ID: 38839139
hey when i put this code in the script it does not work but when i take it out it works


        var hook = true;
        window.onbeforeunload = function() 
        {
            if (hook) 
            {
                var x = document.getElementById('<%=lblArea.ClientID%>').innerText;
                return 'You have not verified your Bed and Breakfast in '+ x +', are you sure you wish to navigate away from this page? email sent to close page without verify:';
            }
        }
        function unhook() 
        {
            hook=false;
        }

Open in new window

0
 

Author Comment

by:JCWEBHOST
ID: 38839144
and i have tested this code in google chrome, it does not work but work for IE

        function doSomething(e) 
        {
            var posx = 0;
            var posy = 0;
            if (!e) var e = window.event;
            if (e.pageX || e.pageY) 
            {
                posx = e.pageX;
                posy = e.pageY;
            }
            else if (e.clientX || e.clientY) 
            {
                posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
                posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
            }
            if (posy < 0) 
            {
                    window.open('emails.aspx', '_blank');
                    return true;
            }
        }

        document.onkeydown = keydown;
        function keydown(evt) 
        {
            if (!evt) evt = event;
            if (evt.altKey && evt.keyCode == 115) 
            { 
                window.open("emails.aspx", '_blank');
            }
        }

Open in new window

0
 
LVL 5

Expert Comment

by:indrajitmahajan
ID: 38839154
Hi..

YEs its just rendering issue of browsers..
Sorry but right now i dont have solution for other browsers..
I will check it..
0
 

Author Comment

by:JCWEBHOST
ID: 38839173
thanks
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 38843152
Aren't you looking for the onunload event...?
<body onload="alert('Something');" onunload="alert('Otherthing');">

Open in new window

0
 

Author Comment

by:JCWEBHOST
ID: 38850387
I've requested that this question be closed as follows:

Accepted answer: 0 points for JCWEBHOST's comment #a38838872

for the following reason:

it works
0
 

Author Closing Comment

by:JCWEBHOST
ID: 38850388
thanks
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month11 days, 13 hours left to enroll

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question