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?

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.

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

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('http://www.msn.com','_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 LastMitch

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.

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.