In JS i tried something like this,

window.onbeforeunload = function() {
    window.open('','_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?

Dani AI

Generated

What you are trying to do (silently redirect or open a new window when the tab closes) is blocked by modern browsers. The beforeunload event can only show a browser-controlled confirmation; custom text is ignored, and you cannot force a redirect from it. During unload, UI APIs such as window.open/alert/confirm are deliberately ineffective, so popups or redirects triggered there will not run. Also, window.open is gated by transient user activation (a real user gesture), which an unload handler does not provide. You also cannot reliably distinguish tab close vs. navigation vs. reload; all of them trigger the same unload lifecycle. ’s instinct back in 2013 was right. (developer.mozilla.org)

Practical pattern: redirect to Thanks.aspx when the user finishes the flow (e.g., form submit), not when they close the tab. In ASP.NET, do the redirect on the server after the successful action (and do not attach unload handlers in the master page, as noted). If you were hoping to sign users out on close, prefer session timeout or an explicit Logout button; relying on browser-close is not dependable, and Session_OnEnd only fires with InProc session state. (learn.microsoft.com)

If you only need to record that the user left, use a lightweight beacon when the page becomes hidden. This works across close, navigate, and refresh without blocking the UI:

<script>
document.addEventListener('visibilitychange', () => {
  if (document.visibilityState === 'hidden') {
    const data = JSON.stringify({ path: location.pathname, ts: Date.now() });
    navigator.sendBeacon('/api/leave', data);
  }
});
</script>

The visibilitychange event reliably fires as the tab is hidden, and sendBeacon is designed for exactly this kind of fire-and-forget logging at unload time. (developer.mozilla.org)

Recommended Answers

All 15 Replies

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.

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?

$(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?

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.

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

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.

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 :'(

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.

Does somebody could help me solve?

try this

window.onbeforeunload = function() {
if(confirm('close?')){
    window.open('','_self');
    }
    return false;
}

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.

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?");
});
Member Avatar for Member #949455

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?

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.