Reputation: 1521
Pulling screen resolution with easy JavaScript Document Write. Using ColdFusion 9.0.
I can get proper numbers etc. But they are not "numbers" must be coming as text. As I cannot use as a number variable...
The code gets me: Available browser width: 1366 Available browser height: 728
And error of: The value document.write(screen.availWidth); cannot be converted to a number.
Any ideas of how to simply convert these to numbers I can work with... Without putting into a form and resubmitting through a form???
Thx
<cfset screenwidth = "<script>document.write(screen.width);</script>" >
<cfset screenheight = "<script>document.write(screen.height);</script>" >
<cfset availWidth = "<script>document.write(screen.availWidth);</script>" >
<cfset availHeight = "<script>document.write(screen.availHeight);</script>">
<cfoutput>
<div style="padding:15px;">
Available browser width: #availWidth#<br />
Available browser height: #availHeight#<br />
</div>
</cfoutput>
<cfset nw = availWidth * 2>
<cfset nh = availheight * 2>
<cfoutput>#nw# and #nh#</cfoutput>
Upvotes: 0
Views: 575
Reputation: 284
I used to have problems with this. Right now, as far as CF is concerned, your availWidth variable contains a string variable which just happens to be a javascript fragment. CF doesn't evaluate this (because it can't) and passes the text as-is to the web server (when you invoke #availWidth#) and thus to the browser. The browser sees that the text is javascript, and evaluates it as 1366; but as far as CF is concerned, it's simply text—which is why you get an error trying to multiply by 2.
The only way to make the CF server aware of the javascript variable is to pass the 1366 back to another page via (e.g.) an Ajax request. But then your current CF template isn't aware of it.
Ben Nadel has a great discussion of the problem.
Upvotes: 4