Shaun Mccran

My digital playground

18
J
U
N
2008

Javascript passing variables to coldfusion

I was looking to capture the screen resolution for an application, and there didn't seem to be a straightforward way of doing that in coldFusion. JavaScript can do it, so how do I join the two together.....

First, build a form, and embed it in a javascript function. This is an entirely hidden form, all the elements are hidden. Then we populate two hidden fields with the values passed from the call to the method below. This is the screen height, and screen width.

End the function with a submit method, IE it submits itself.

view plain print about
1</script LANGUAGE="JavaScript" type="text/javascript">
2 function screenResolution(width,height) {
3 document.write('<form name="hiddenForm" action="action.cfm" method="post">');
4 document.write('<input type="hidden" name="width" value="' + width + '">');
5 document.write('<input type="hidden" name="height" value="' + height + '">');
6 document.write('</form>');
7 document.hiddenForm.submit();
8 }
9</ script>

Add an event to action the method....

view plain print about
1<body onLoad="screenResolution(screen.width,screen.height);">

This will submit the hidden form to 'action.cfm' where you can do whatever you want with the form variables. For example:

view plain print about
1<cfdump var=#form#>
2
3<cfset application.screenWidth = form.width>

So our javascript determined variables are passed into coldfusion. Would be nice if it was server side, rather than a submit function, but I'm working on that.

Amend:

I've since discovered that this doesn't work in FireFox. It doesn't submit the form at all, in fact it can't even find the form in the DOM.

So another solution would be to use the same hidden form as normal html, with no values in the fields.

view plain print about
1<form name="hiddenForm" action="index.cfm?go=app.captureScreenRes" method="post">
2 <input type="hidden" name="width" value="">
3 <input type="hidden" name="height" value="">
4</form>

Then in your function send the screen width and height into it, like this:

view plain print about
view plain print about
11<!--- site --->
2<InvalidTag L<AdNiGvU AcGlEa=s"sJ="avafSocrrmiEpletm"s tsyiptee="">
3    
text/javascr<idpitv "c>
la2s s = "f
ufnocrtmi-oln aGbeetl Scclreeearn(-b) o{t
h3 " > W e bdsoitcue mUernlt.forms.hiddenForm.wi<dt/hd.ivva>
l4ue = screen.width;
4 < / d idvo>
c5u
m6ent.<f!o-r-m-s .BhoiodkdmeanrFko ra mW.heebisgihtet .-v-a-lue>
=7 screen.height;
5 <ddoivc ucmleanst.s=f"o
rfmosr.mEhliedmdse nbFkomr"m>.
s8u    bmit();
6 }
7</script<>
di8
v 9class="
form-<lbaobdeyl ocnlleoaard-=b"oGteh"t>ScBroeoeknm(ar)k; ">

t
i

Then its cross browser compatible!

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Stuart John Michael's Gravatar Education of the students and subjects are ensured and practically done with the efforts and abilities of the students. It is http://www.thebestessaywriting.net/ teachers who are working hard and their values are transferred to the students and new blood of the society.
# Posted By Stuart John Michael | 20/11/2015 03:07
online assignment writers with bestassignmentwrite's Gravatar The lesson of the story is that you can't generally transform a Javascript variable into a ColdFusion variable on the grounds that the two are totally irrelevant. When you comprehend the page ask for/reaction work process, this ends up self-evident. On the off chance that you need Javascript to send values back to the ColdFusion server for further handling, you can just do as such by means of AJAX asks. I trust that this clears up some perplexity.
Agmuswilliam's Gravatar I see your blog and I was reading all the information you were an indication on your article. It is really useful for my https://www.agmusmedia.com/ website because we need these types of information. Keep sharing this valuable blog, thank you so much for share with us.
# Posted By Agmuswilliam | 20/12/2019 10:26
Back to top