Michael Robinson
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-w idth: 767px)').matches) - gives me "mobile"
(window.matchMedia('(min-w idth: 768px)').matches && window.matchMedia('(max-wi dth: 1024px)').matches) - gives me "tablet"
(window.matchMedia('(min-w idth: 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.ScreenSi ze")>
<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?
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-w
(window.matchMedia('(min-w
(window.matchMedia('(min-w
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.ScreenSi
<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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
AJAX can work because it satisfies the requirement for another page request to the server. And cookies are sent with every page request.
ASKER