0

In JS i tried something like this,

window.onbeforeunload = function() {
    window.open('http://www.msn.com','_self');
    return 'close?';
}

but it doesn't redirect the page, but the return statement is displayed.
how can i do to archieve redirect a specify page without any alert?

6
Contributors
15
Replies
56
Views
4 Years
Discussion Span
Last Post by sunil_17
Featured Replies
  • My hobby is love to apply those very interesting ideas like difficult/impossible to apply. But as long as i know, ## I NEVER TRY, I'LL NEVER KNOW THE RESULT. ## *THIS IS THE MEANING OF ENJOY THE CODING.* Read More

0

window.beforeunload utilizes a method built in to the browser. It looks like according to MDN, you can return a string as you did in your example, and the browser will display the string. The user will be prompted for an action.

Based on this information, I do not beleive that you can redirect the user to another page.

0

Then how can i arhieve my requirement?
Doesn't matter whether is display a string message or not, if it do, then what condition should i use to detect the user if confirm to close?

0
$(document).ready(function() {    
  window.onbeforeunload = beforeUnload;
  window.onunload = afterUnload;
});

function beforeUnload() {
    return "You are attemped to leave the page. Are you sure want to leave MSJ Bank Financial?";
}

function afterUnload() {
    params  = 'width='+screen.width;
    params += ', height='+screen.height;
    params += ', top=0, left=0'
    params += ', fullscreen=yes';
    window.open('/Thanks.aspx', '_newtab', params);
    window.focus();
}

Put this code into .js file and link you should able to open a new window.
And those params are attempted to force the new window to full screen of your window.

But now the problem i faced is whenever i in my master page call another child page (under same master page) also will redirect me to this Thanks.aspx. how to solve?

Edited by gahhon: Problem faced again.

0

whenever i in my master page call another child page (under same master page) also will redirect me to this Thanks.aspx. how to solve

Yes so the problem is that the browser is not distinguishing the difference from a button, hyperlink, or closing of the browser. They all produce the unload related events. So placing this in your master page, doesnt sound like an appropriate solution.

In any event, if you are thanking your user, that would seem to me that the user has completed some type of process, possibly ordering from your catalog, or registering for you site, etc...

If that is the case, you wouldnt include this JS on the master page, but you would only include it in one single page...the last page in the checkout/registration/etc... The user finishes interacting with you, they click to close, or close the browser and your script runs.

I dont know how you are going to distinguish the user actions if you do this from the master page which affects all pages.

0

i think what is your meaning, but is that possible to force user redirect to logout pages without trigger any unload event?

0

is that possible to force user redirect to logout pages without trigger any unload event

yes, you can redirect on other asp.net events as you are aware such as the page load, click, etc...

But you are looking for DOM events because you want to detect when the user closes the page I assume. Unfortuantely that event handles the same action when you visit another page because the current page closes....hmmm, I'm not certain what other options you can look at to help you redirect a depending on whether the page is closed vs opening a new page.

I'll continue to investigate options..your request is interesting.

0

Solution

i found this website, the problem faced same as mine.
But he is worked, i tried those response users' code, is doesn't work for me.
event.clientX < 0 || event.clientY < 0 :'(

1

My hobby is love to apply those very interesting ideas like difficult/impossible to apply. But as long as i know,

I NEVER TRY, I'LL NEVER KNOW THE RESULT. ##

THIS IS THE MEANING OF ENJOY THE CODING.

0

Thanks alvin, but it's doesn't work.
i just want some tricks to detect the user is exactly close the web tab or browser but not go another location of url.

0

Your code is going to fire as soon as the page is finished loading.

Try this, it works differently on IE and Firefox though:

$(window).bind('beforeunload', function() {
    alert("You are attempting to leave this page. Are you sure want to leave MSJ Bank Financial?");
});

Edited by ggamble

0

but it doesn't redirect the page, but the return statement is displayed. how can i do to archieve redirect a specify page without any alert?

@gahhon

Initially, I thought this was a ASP.net issue because it does caught my intention but now it looks like a javacript issue.

0

Still unable to do it. Never mind, plan has been canceled.
Thanks for helping so much.

0

hi dani,
pliz resolve that use custome message or custome popup box when close tab or browser using window.onbeforeunload or anything....

please.....

from
sunil chandrawansi
email:-sagar00sunil@gmail.com
mobile:-+91 7503688037

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.