Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Regular Coder stevenmw's Avatar
    Join Date
    Jun 2007
    Location
    OK
    Posts
    497
    Thanks
    27
    Thanked 31 Times in 31 Posts

  • #3
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Is there a way to redirect the user only after they have initiated a refresh?

  • #4
    Regular Coder stevenmw's Avatar
    Join Date
    Jun 2007
    Location
    OK
    Posts
    497
    Thanks
    27
    Thanked 31 Times in 31 Posts

  • Users who have thanked stevenmw for this post:

    HimeAnator (02-28-2012)

  • #5
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #6
    Regular Coder stevenmw's Avatar
    Join Date
    Jun 2007
    Location
    OK
    Posts
    497
    Thanks
    27
    Thanked 31 Times in 31 Posts
    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.";


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •