Welcome to Dream.In.Code
Getting Help is Easy!

Join 109,336 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,021 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Inserting a JavaScript Value into ColdFusion

 
Reply to this topicStart new topic

Inserting a JavaScript Value into ColdFusion

ryanaghdam
post 1 Jul, 2008 - 04:38 AM
Post #1


New D.I.C Head

*
Joined: 14 Jun, 2008
Posts: 8


My Contributions


I am trying to insert a value from javascript into a database to log visitors. So far, I have the following:

CODE

<cfsavecontent variable="UserAgent">
  <cfoutput>
    <script type="text/javascript">
      var x = navigator;
      return x.userAgent;
    </script>
  </cfoutput>
</cfsavecontent>

<!--- log the visit --->
<cfquery name="LogVisit" datasource="Announce">
  INSERT INTO Visits
    (VisitTarget, VisitHost, VisitUserAgent)
  VALUES
    (#id#, '#CGI.REMOTE_HOST#', '#UserAgent#')
</cfquery>


The actual JavaScript code is inserted into the database.

Another note, I am aware of the CGI.USER_AGENT (the exact name may be off), but I was unhappy with the results it produces.


Thanks in advance.
User is offlineProfile CardPM

Go to the top of the page


sansclue
post 1 Jul, 2008 - 10:56 AM
Post #2


D.I.C Head

**
Joined: 21 Nov, 2007
Posts: 82



Thanked 4 times
My Contributions


QUOTE(ryanaghdam @ 1 Jul, 2008 - 04:38 AM) *

The actual JavaScript code is inserted into the database.


Unfortunately you cannot grab a javascript value from ColdFusion that way.

ColdFusion code runs on the server, and it runs first. The server essentially knows nothing about javascript or browser settings. The CF code is executed, converted to HMTL and finally sent to the browser. So any javascript code runs in the browser, after the ColdFusion code has already finished executing.

You need to send the javascript value from the client (browser) back to the server. For example, on page1 use javascript to store the value in a hidden form field. The value will be accessible on page 2, so you can grab it and store it in your database. Or use option like ajax, but the idea is the same.

User is offlineProfile CardPM

Go to the top of the page

ryanaghdam
post 1 Jul, 2008 - 12:21 PM
Post #3


New D.I.C Head

*
Joined: 14 Jun, 2008
Posts: 8


My Contributions


QUOTE(sansclue @ 1 Jul, 2008 - 10:56 AM) *

QUOTE(ryanaghdam @ 1 Jul, 2008 - 04:38 AM) *

The actual JavaScript code is inserted into the database.


Unfortunately you cannot grab a javascript value from ColdFusion that way.

ColdFusion code runs on the server, and it runs first. The server essentially knows nothing about javascript or browser settings. The CF code is executed, converted to HMTL and finally sent to the browser. So any javascript code runs in the browser, after the ColdFusion code has already finished executing.

You need to send the javascript value from the client (browser) back to the server. For example, on page1 use javascript to store the value in a hidden form field. The value will be accessible on page 2, so you can grab it and store it in your database. Or use option like ajax, but the idea is the same.



Submitting it via form was the only way I could do it. I had it scripted so it would send the values to a form processor that would open in a new window. The new window would automatically submit then close. It seemed a little hack-y but it got the job done.

Thanks for your help, avecclue.
User is offlineProfile CardPM

Go to the top of the page

sansclue
post 2 Jul, 2008 - 11:16 AM
Post #4


D.I.C Head

**
Joined: 21 Nov, 2007
Posts: 82



Thanked 4 times
My Contributions


QUOTE(ryanaghdam @ 1 Jul, 2008 - 12:21 PM) *

Submitting it via form was the only way I could do it. I had it scripted so it would send the values to a form processor that would open in a new window. The new window would automatically submit then close. It seemed a little hack-y but it got the job done.

Thanks for your help, avecclue.


You're welcome.

Yes, I hear you. But it is just the nature of the server/client relationship. An ajax solution might be a slightly more elegant. But essentially it would be doing the same thing: submitting information from the browser (page1) to the server (page2). Other than using server side CGI variables, I think that is about as good as it gets ;-)
User is offlineProfile CardPM

Go to the top of the page

wiredwizard
post 4 Jul, 2008 - 10:01 AM
Post #5


D.I.C Head

Group Icon
Joined: 7 May, 2005
Posts: 72



Dream Kudos: 225
My Contributions


HTTP_USER_AGENT value could be used in an if else type of function to assign a value
CODE
<CFIF FindNoCase('Safari','#CGI.HTTP_USER_AGENT#') GREATER THAN 0>
  <CFSET browser = 'Safari'>
</CFIF>
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 9/6/08 04:14PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month