shiva
shiva

Reputation: 2770

Differentiate Between Meta Refresh ans other refresh or page unload events

I have a situation where i will redirect the user using to another page if he stays in the page for X seconds.

I m using Meta Refresh tag for this.

<meta http-equiv="refresh" content="600; URL={{redirectURL}}">

Also i want to show native window to the user if he tries to refresh or leave the page before X seconds other than submit the form in the page.

I m using the below script to show the native window.

<script type="text/javascript">
$(function(){
var submitted = false;
$(".cancel_submit").on('click',null,function (e){
submitted=true;
});

window.onbeforeunload = function()
{
if(!submitted && gbl_time>15)
{
str = "This is a secure page. Leaving this page will end your current session. Are you sure you want to leave?.";
return str;
}
submitted=false;
}

});

But the native is shown for meta refresh also?How can i avoid it?

Upvotes: 0

Views: 348

Answers (1)

mdunisch
mdunisch

Reputation: 3697

Btw: Your Meta-refresh is called after 600 Secounds??

Your Problem: Meta-refresh is fire the window.onbeforeunload-event because loading a URL is a unload of the current webseite. Unfortunately the window.onbeforeunload-event don't give you a hint, what event exact happened.

Use a Javascript-function to set the redirect instead of Meta-refresh:

var callfromwait = false;

window.onbeforeunload = function() {
   if(!callfromwait){
     return "Sure, you want to leave our website?";   
   }
};

window.setTimeout(function(){
 callfromwait= true;
 window.location.href = "http://www.test.de";
}, 5000); // milliseconds

Demo: http://jsfiddle.net/65wsm/

Upvotes: 1

Related Questions