Link to home
Start Free TrialLog in
Avatar of Michael Robinson
Michael RobinsonFlag for United States of America

asked on

How to get javascript to talk to coldfusion in < 3 page loads

I am trying to use javascript running in browser on a coldfusion page to detect the screen size (mobile, tablet, desktop) and pass that to coldfusion via a cookie.  Then CF to write it into a database.  

I'm using a cookie to bridge the gap betwqeen JS and CF.  But the visitor has to hit 3 pages before I get the data into the database.

Would like to do it in one page or worst case two pages.  

I'm running CF 9, and can use JS, jQuery and Bootstrap.

I heard CF 2016 can do this screen size detection, but that's a lot of work and $$ just to get this one function to work.

So here is what is working but takes 3 page loads.

1) JavaScript on each page, in the footer that determines screen size using

 (window.matchMedia('(max-width: 767px)').matches) - gives me "mobile"
(window.matchMedia('(min-width: 768px)').matches && window.matchMedia('(max-width: 1024px)').matches) - gives me "tablet"
(window.matchMedia('(min-width: 1025px)').matches )  - results in "Desktop"

Then, JS writes a cookie called "ScreenSize" with the result - mobile, tablet, desktop

2) on the second page load CF reads the cookie in the application.cfm file using
<cfif isDefined("cookie.ScreenSize")>
 <cfset Session.ScreenSize = #cookie.ScreenSize#>
<cfelse>
</cfif>

3) On the third page load CF writes the screen size to the database in the application.cfm file.

I don't know why CF does not write to the database on page load 2 when it does know what the cookie says.

If I can fix that and get the screen size into the database on 2 page loads that would be better than what I have now.

But what I really need is to do it in one page load, because some of these visitors may only see one page.

Any advice?
SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Michael Robinson

ASKER

thx  gdemaria  i will try that ajax approach   been wanting a reason to learn ajax.
AJAX can work because it satisfies the requirement for another page request to the server.  And cookies are sent with every page request.