Avatar of ecpeel
ecpeel
 asked on

instead of history.go

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
JavaScriptColdFusion Language

Avatar of undefined
Last Comment
ecpeel

8/22/2022 - Mon
gdemaria

Use cflocation to redirect to the page
ecpeel

ASKER
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 ...
gdemaria

> 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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ecpeel

ASKER
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.
gdemaria

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.
ecpeel

ASKER
you mean convert the the entire function to javascript so all validation takes place on the client?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
gdemaria

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ecpeel

ASKER
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?
ecpeel

ASKER
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

ecpeel

ASKER
Thanks gdmaria
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck