Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing javascript variables to coldfusion? 1

Status
Not open for further replies.

xor

Programmer
Jan 7, 2001
71
NZ
Is there a way to pass javascript variables to coldfusion?
I want to get info like screen size and color depth and put them into a stats database (which also counts hits to my website). I can get some of the info I want to gather from coldfusion (like user_agent) but the rest I think I will have to get from javascript (any other suggestions?). The problem is, how do I then get the info into my database? Is there a way to pass javascript variables into coldfusion? I have used JSStringFormat to pass data from coldfusion to javascript, but I'm having a hard time passing data the other way (javascript --> coldfusion). Any ideas?

TIA,
xor
 
Xor,

I haven't checked this out in detail but the following approach might help:

Write a javascript which will:
* collect the info you want.
* use document.writeln() to build a FORM dynamically with the ACTION calling your receiving CF template and the INPUTs providing the names and values expected by the receiving template.
* submit the form via myForm.submit()

The tricky bit is finding a way to do this without disrupting the normal user dialogue. For example if the user clicks a link or button on the page before the form has been completely sent, the browser will cancel the submit and perform whatever action the user requested. So you may have to:
* create a special start page containing the javascript and an explanation of what's going on (but no buttons or links), and rejig the rest of your site / app to make this the automatic start page.
* make the receiving template initiate display of the first "real" page when it's finished processing the data you want to capture.

Hope this helps.
 
xor
To pass a JavaScript variable value to CF siimply declare the variable as a Global Variable in JavaScript first. (i.e., var sample = 0; be sure to declare it outside of a
function but still inside the <script> </script> in the <head> <head> portion of the
.cfm page. Then inside the function set the value to what it should be and then reference it inside CF.

pjbarteck
 
It's somewhat easy to (in a roundabout way) have Javascript affect a ColdFusion page ... trouble is, the others have been right. You need to submit a form to do so.

In a content management system I'm just finishing, I just this evening cobbled together a browser detection script which determined if my once-you're-logged-in CF page should use certain DOM goodies or not. The script in the login page's <head></head> is:
[COLOR=880000]
<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
<!--
var n4 = (document.layers) ? 1 : 0;
var ie = (document.all) ? 1 : 0;
var dom;
var browser = (navigator.appVersion.indexOf(&quot;MSIE&quot;) >= 0) ? 1 : 0;

if (n4 == 1) {dom = 0}
else {
if (navigator.userAgent.indexOf(&quot;MSIE 4&quot;) == -1){
(document.getElementsByTagName(&quot;*&quot;)) ? (dom = 1) : (dom = 0)
}
}
[COLOR=ff0000]
function testbrowser() {
if ((dom == 1)&&(browser == 1)) {
document.write('<input type=&quot;hidden&quot; name=&quot;DOMtest&quot; value=&quot;yes&quot; />')
}
else {
document.write ('<input type=&quot;hidden&quot; name=&quot;DOMtest&quot; value=&quot;no&quot; />')
}
}
[/color]
//-->
</script>[/color]

[COLOR=555555](The above script pretty much tests for the IE5+. Lest you harangue me for being a Microsoft exclusivist, this actually allows the app to be fully functional in all browsers. You'll see how in a moment ...)[/color]

Then the login form contains this part:
[COLOR=880000]
<form action=&quot;index.cfm?choice=login&quot; method=&quot;post&quot;>
...other form elements ...
[COLOR=ff0000]
<script language='javascript' type='text/javascript'>
<!--
testbrowser()
//-->
</script>[/color]
<input type=&quot;submit&quot; value=&quot;Log in&quot; />
</form>[/color]

As you've probably figured out, the JS in the head determines which hidden form element to add to the form. That value (either &quot;yes&quot; or &quot;no&quot;) then gets used in the page that the form submits to:
[COLOR=008800]
<cfif form.DOMtest is &quot;yes&quot;>
<p class=&quot;confirmation&quot;>You're in and DOM-enabled.</p>
<cfset session.DOM = &quot;true&quot;>
<cfelse>
<p class=&quot;error&quot;>No DOM for you.</p>
<cfset session.DOM = &quot;false&quot;>
</cfif>[/color]

Ta-da: a Javascript function affects a following CF page.

[COLOR=555555](Oh yeah, the &quot;why test for IE5+?&quot; question. With some of the DOM-interacting-with-CSS stuff I did, all versions of Netscape [even 6.2] and older IE just didn't respond, which made certain necessary form and page editing elements unusable. Thus I'm putting some <cfif>s around those DOM bits: if it's a DOM-OK browser, then it'll get the cool little interactive CSS trickeries; if it's not, then it'll just get the form/editing elements without any fuss. Make sense?)[/color]
 
One of the things that I do is create a hidden frame (create a frame with a 0 height) and you can change the location of the hidden frame to a cf page that will do the updates for you.

An iframe will work well too..

e.g


<script language=&quot;javascript&quot;>
function sendStats() {
submitframe.location.href = 'somefile.cfm?var1=something';
}
</script>

<body onload=&quot;sendStats()&quot;;>

<iframe name=&quot;submitframe&quot; width=&quot;0&quot; height=&quot;0&quot;>

Then somefile.cfm will get your variables as a url variables and you can process and insert data, etc. The user will never see it. I use this method all the time to change information on the screen without having to reload the entire page.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top