instead of history.go

ecpeel
ecpeel used Ask the Experts™
on
Hi [having a brain freeze],

On the page that sends to the item details page (display search results) are the search results.
On this page I trigger a Survey and set a cookie to say the Survey has been taken (onclick select for details).
On an item details page we're using a history.go(-1) as a "back" button.

Problem: if you use the back button the page doesn't refresh to run the logic to enable or disable the code to trigger the survey. Of course a fresh search triggers the code.

any ideas about how to get that page to reload and run this code?

Thanks in advance
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Use cflocation to redirect to the page

Author

Commented:
Hi gdmaria,
I don't think that will work. The page where the search critieria was before itemsdisplay page.
I'll need to activate the "check" as a call to a page external to the stateless page so that regardless it always does a validation.
It's been mentioned that it could be a ajax call the page that holds the javascript function and the cflogic.
I guess something like inclick = myFunc
and inside of myFunc is the call to the validation data
 $ajax(url:validationcode.cfm);

something like that ...
> I'll need to activate the "check" as a call to a page external to the stateless page so that regardless it always does a validation.

sorry, this is unclear

Is the validation you are performing server side?  (Coldfusion?)

If so, what is triggering that validation, a particular form post?

If so, you can form post back from your itemDisplay page to your itemSearch page.

But, personally, I would never reply on using back or go(-1) to navigate between pages.
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
the validation is server side and looks like this:

function showSearchSurvey(){
<cfif structkeyexists(cookie,'searchResultsSurvey.clickedSurvey')>
	<cfset variables.killSurvey = 1>
	<cfelse>
	<cfset variables.killSurvey = 0>
	</cfif>
	<cfif variables.killSurvey EQ 0>
window.open('https://www.surveymonkey.com', 'invitationwindow', 'height=350,width=500,scrollbars=yes,resizable=yes,status=yes');			
<cfcookie name="searchResultsSurvey.clickedSurvey" value="Yes"expires="never" 
/>
</cfif>	
}

Open in new window

It  is triggered by onclick when a user wants to see the details of a item returned in the search
No - I can't form post back
Using the go(-1) sux especially in this case.
Hmm, that's really not server-side validation, unless I am not reading it correctly.

It's appears to be a javascript function that has embedded Coldfusion in it.   Once the server-side process has completed, you are left with just a js function that opens a window.   If killsurvey ends up to be 1 then the function will do nothing.

It may help, you can throw out the coldfusion check for the cookie and use a javascript check for the cookie instead.  Basically, the function is saying if cookie does not exist set a cookie and open the window.  If the cookie doesn't exist, do nothing.   That can be done in javascript if you want.

Author

Commented:
you mean convert the the entire function to javascript so all validation takes place on the client?
> you mean convert the the entire function to javascript so all validation takes place on the client

It appears that the function IS javascript, with coldfusion mixed in.   After CF processes it, all that is left after the page loads is javascript.   I'm saying you don't need the few lines of ColdFusion in that function because javascript can also check the existance of a cookie and create a cookie.  That's the only part of the code that is coldfusion.

Stepping back, the onClick function is ultimately calling the javascript window.open() function.  That's really what is happening there.   You use Coldfusion to check to see if a cookie is set in order to allow that to happen.


As an option, when drawing the page, check for the cookie..

When the user clicks it, set the cookie via js..

<cfif NOT isDefined("cookie.searchResultsSurvey.clickedSurvey")>
 <a hred="" onClick="window.open('https://www.surveymonkey.com', 'invitationwindow', 'height=350,width=500,scrollbars=yes,resizable=yes,status=yes');SetCookie('searchResultsSurvey.clickedSurvey','Yes');">Click Me</a>
</cfif>


Here's a javascript function to set a cookie..

function SetCookie(cookieName,cookieValue,nDays) {
  var today = new Date();
  var expire = new Date();
  if (nDays==null || nDays==0) nDays=1;
  expire.setTime(today.getTime() + 3600000*24*nDays);
  document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
  }

Open in new window

Author

Commented:
Looks reasonable.
But won't the same issue exist (persist) because of using history.go as a back button instead of re-running the search?
By using history.go (the real culprit), the CF code will never check for the existence of the cookie even if the script is in the onclick event?

Author

Commented:
Ok, here's what I ended up doing that worked. Inspired by you and including your code mixed with another's found http://stackoverflow.com/questions/5968196/check-cookie-if-cookie-exists and what I relearned is that CF even tucked inside of a javascript function only runs when the page loads. Thanks as always gdmaria

	<script>
			  
		function getCookie(name) { 
		    var dc = document.cookie; 
		    var prefix = name + "="; 
		    var begin = dc.indexOf("; " + prefix); 
		    if (begin == -1) { 
		        begin = dc.indexOf(prefix); 
		        if (begin != 0) return null; 
		    } 
		    else 
		    { 
		        begin += 2; 
		        var end = document.cookie.indexOf(";", begin); 
		        if (end == -1) { 
		        end = dc.length; 
		        } 
		    } 
		    return unescape(dc.substring(begin + prefix.length, end)); 
		}  
		
	
		function showSearchSurvey(){
				
			    var myCookie = getCookie("searchResultsSurvey.clickedSurvey"); 
			 
			    if (myCookie == null) { 
			        // do cookie doesn't exist stuff; 
			        window.open('https://www.surveymonkey.com/jsPopInvite.aspx?sm=03kYcE5oyzMT7O5A7k0E8w%3d%3d', 'invitationwindow', 
								'height=350,width=500,scrollbars=yes,resizable=yes,status=yes');		
						
					SetCookie('searchResultsSurvey.clickedSurvey','Yes');		
					
			    } 
			    else { 
			        // do cookie exists stuff or nothing at all
			        //alert('yeah its found');
			    } 
			}

		function SetCookie(cookieName,cookieValue,nDays) {
			  var today = new Date();
			  var expire = new Date();
			  if (nDays==null || nDays==0) nDays=1;
			  expire.setTime(today.getTime() + 3600000*24*nDays);
			  document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
			  }
	</script>

Open in new window

Author

Commented:
Thanks gdmaria

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial