Go Back   CodingForums.com > :: Client side development > HTML & CSS

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-27-2012, 08:50 PM   PM User | #1
HimeAnator
New to the CF scene

 
Join Date: Feb 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
HimeAnator is an unknown quantity at this point
Question Redirect User If They Refresh A Page

How would I redirect a user to the home url if they try to refresh the page they are on? Not sure if this would be done in HTML or PHP because it’s a wordpress based website. Thanks for any help!
HimeAnator is offline   Reply With Quote
Old 02-28-2012, 03:07 AM   PM User | #2
stevenmw
Regular Coder

 
stevenmw's Avatar
 
Join Date: Jun 2007
Location: OK
Posts: 446
Thanks: 26
Thanked 30 Times in 30 Posts
stevenmw is an unknown quantity at this point
http://able2know.org/topic/3000-1
stevenmw is offline   Reply With Quote
Old 02-28-2012, 03:22 AM   PM User | #3
HimeAnator
New to the CF scene

 
Join Date: Feb 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
HimeAnator is an unknown quantity at this point
Is there a way to redirect the user only after they have initiated a refresh?
HimeAnator is offline   Reply With Quote
Old 02-28-2012, 03:28 AM   PM User | #4
stevenmw
Regular Coder

 
stevenmw's Avatar
 
Join Date: Jun 2007
Location: OK
Posts: 446
Thanks: 26
Thanked 30 Times in 30 Posts
stevenmw is an unknown quantity at this point
http://www.tedpavlic.com/post_detect...javascript.php
stevenmw is offline   Reply With Quote
Users who have thanked stevenmw for this post:
HimeAnator (02-28-2012)
Old 02-28-2012, 08:10 PM   PM User | #5
HimeAnator
New to the CF scene

 
Join Date: Feb 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
HimeAnator is an unknown quantity at this point
Thank you so much it works!

I got it to work using this code:
Code:
<html>
<head>

<script language="JavaScript"> <!--
function checkRefresh()
{
	// Get the time now and convert to UTC seconds
	var today = new Date();
	var now = today.getUTCSeconds();

	// Get the cookie
	var cookie = document.cookie;
	var cookieArray = cookie.split('; ');

	// Parse the cookies: get the stored time
	for(var loop=0; loop < cookieArray.length; loop++)
	{
		var nameValue = cookieArray[loop].split('=');
		// Get the cookie time stamp
		if( nameValue[0].toString() == 'SHTS' )
		{
			var cookieTime = parseInt( nameValue[1] );
		}
		// Get the cookie page
		else if( nameValue[0].toString() == 'SHTSP' )
		{
			var cookieName = nameValue[1];
		}
	}

	if( cookieName &&
		cookieTime &&
		cookieName == escape(location.href) &&
		Math.abs(now - cookieTime) < 5 )
	{
		// Refresh detected

		// Insert code here representing what to do on
		// a refresh
		window.location = "http://www.regameit.com"
		// If you would like to toggle so this refresh code
		// is executed on every OTHER refresh, then 
		// uncomment the following line
		// refresh_prepare = 0; 
	}	

	// You may want to add code in an else here special 
	// for fresh page loads
}

function prepareForRefresh()
{
	if( refresh_prepare > 0 )
	{
		// Turn refresh detection on so that if this
		// page gets quickly loaded, we know it's a refresh
		var today = new Date();
		var now = today.getUTCSeconds();
		document.cookie = 'SHTS=' + now + ';';
		document.cookie = 'SHTSP=' + escape(location.href) + ';';
	}
	else
	{
		// Refresh detection has been disabled
		document.cookie = 'SHTS=;';
		document.cookie = 'SHTSP=;';
	}
}

function disableRefreshDetection()
{
	// The next page will look like a refresh but it actually
	// won't be, so turn refresh detection off.
	refresh_prepare = 0;

	// Also return true so this can be placed in onSubmits
	// without fear of any problems.
	return true;
} 

// By default, turn refresh detection on
var refresh_prepare = 1;

-->
</script>

</head>

<body onLoad="JavaScript:checkRefresh();" onUnload="JavaScript:prepareForRefresh();">

<!-- The above is all that is needed. -->

<!-- Below is an example of the use of disableRefreshDetection() 
     to prevent false refreshes from being detected when forms 
     are submitted back to this page. If your web page does
     not use forms that post back to themselves, then the
     below does not matter to you. Omit it. -->


</body>
</html>
How would I ad a message to this asking the user if they are sure they want to refresh? such as:
Code:
<script type = "text/javascript">
var needToConfirm = true;
window.onbeforeunload = confirmExit;
function confirmExit() {
if (needToConfirm) {
return "You have attempted to refresh or leave this page. All ad data will be reset and you will have to restart the posting process in order for your ad to publish correctly.";
}
}
</script>
But I have had no luck getting the two codes to work together
HimeAnator is offline   Reply With Quote
Old 02-28-2012, 08:32 PM   PM User | #6
stevenmw
Regular Coder

 
stevenmw's Avatar
 
Join Date: Jun 2007
Location: OK
Posts: 446
Thanks: 26
Thanked 30 Times in 30 Posts
stevenmw is an unknown quantity at this point
I'm not fluent in javascript, and i don't think I should be offering help for it in the html forums..

My guess would be


Code:
if( refresh_prepare > 0 )     {         
// Turn refresh detection on so that if this         
// page gets quickly loaded, we know it's a refresh         
var today = new Date();         var now = today.getUTCSeconds();         document.cookie = 'SHTS=' + now + ';';         
document.cookie = 'SHTSP=' + escape(location.href) + ';';     }
That part of your code detects that the page is getting refreshed right?

So couldn't you some how put this in that if statement?

Code:
return "You have attempted to refresh or leave this page. All ad data will be reset and you will have to restart the posting process in order for your ad to publish correctly.";
stevenmw is offline   Reply With Quote
Reply

Bookmarks

Tags
redirect, refresh, wordpress

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:29 AM.


Advertisement
Log in to turn off these ads.