In a previous post I created a CFC to save simple variables into the session scope. In that CFC I had set up two functions, "SetSessionVar" and "GetSessionVar". However I ended up only needing the "SetSessionVar" function for my example so I never really tested the "GetSessionVar" function. Someone asked me today how to invoke it, and that is when I realized there were a couple little problems that I needed to fix.

Here is the updated Session.cfc code, I had to modify the output and returntype attributes in the "GetSessionVar" function:

<cfcomponent name="session" hint="Performs Session Functions">
<cffunction name="SetSessionVar" access="remote" output="false" returntype="Boolean">
<cfargument name="Name" type="string" required="true">
      <cfargument name="Value" type="string" required="true">
            <cfset session[arguments.Name] = arguments.Value>
            <cfreturn true>
</cffunction>
   <cffunction name="GetSessionVar" access="remote" output="true" returntype="String">
<cfargument name="Name" type="string" required="true">
            <cfreturn session[arguments.Name]>
</cffunction>
</cfcomponent>

In my previous post using this CFC, I demonstrated how to execute the SetSessionVar function, but since that function did not return anything it was a tad bit simpler to do. If you are using cfajaxproxy to connect to a CFC function that returns a string or JSON object or something, you will need to set up a callback handler function that will get excecuted if the function is invoked successfully. Then you specify it using the setCallbackHandler function in the CFC proxy object.

Here is my working Save Active Tab Example that now has a button that when clicked will call the GetSessionVar function and use a callback handler that just pops up an alert box with the value of the session variable.

<html>
<head>
<title>Save Active Tab Example</title>

<cfajaxproxy cfc="session" jsclassname="CFCs.session">

<cfset AjaxOnLoad("addTabChangeListener")>

<script language="JavaScript">
addTabChangeListener = function(){
myTabs = ColdFusion.Layout.getTabLayout("MyTabLayout");
myTabs.on('tabchange',SaveActiveTab);
}

SaveActiveTab = function(){
myTabs = ColdFusion.Layout.getTabLayout("MyTabLayout");
actTab = myTabs.getActiveTab();
var SessionObj = new CFCs.session();
      SessionObj.setErrorHandler(errorHandler);
      SessionObj.SetSessionVar("ActiveTab", actTab.id);
}

errorHandler = function(statusCode,statusMsg) {
   alert(statusCode+': '+statusMsg)
}
//Here is the Callback Handler that puts the response into an alert
AlertResponse = function(res){
      alert(res);
}
//Here's how to call the GetSessionVar function
CheckActiveTab = function(){
   var SessionObj = new CFCs.session();
      SessionObj.setErrorHandler(errorHandler);
      SessionObj.setCallbackHandler(AlertResponse);
      SessionObj.GetSessionVar("ActiveTab");
}
</script>

</head>
<body>
<table width="95%" align="center"><tr><td>
<cflayout type="tab" name="MyTabLayout">
   <cfif session.activetab eq "tab1"><cfset isSelected="true"><cfelse><cfset isSelected="false"></cfif>
<cflayoutarea name="tab1" selected="#isSelected#" title="Tab 1" style="height:100%">
   This is Tab 1
</cflayoutarea>
<cfif session.activetab eq "tab2"><cfset isSelected="true"><cfelse><cfset isSelected="false"></cfif>
<cflayoutarea name="tab2" selected="#isSelected#" title="Tab 2" style="height:100%">
   This is Tab 2
</cflayoutarea>
<cfif session.activetab eq "tab3"><cfset isSelected="true"><cfelse><cfset isSelected="false"></cfif>
<cflayoutarea name="tab3" selected="#isSelected#" title="Tab 3" style="height:100%">
   This is Tab 3
</cflayoutarea>
<cfif session.activetab eq "tab4"><cfset isSelected="true"><cfelse><cfset isSelected="false"></cfif>
<cflayoutarea name="tab4" selected="#isSelected#" title="Tab 4" style="height:100%">
   This is Tab 4
</cflayoutarea>
</cflayout>
</td></tr></table>
<input type="Button" value="alert active tab" name="alertactivetab" onclick="CheckActiveTab();">
</body>
</html>

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Todd Rafferty's Gravatar There must be something I'm doing wrong. I attempted to do something similar, only it was a very basic javascript detection script. User would have a session var, by default, session.user.jsenabled = false is set. Use CFAjaxproxy, if it triggered, then set session.user.jsenabled = true. Otherwise, if it remained false, then I could in theory, display a "Hey, you have javascript turned off... you're going to miss out on some features."

I need to re-look over my code and figure it out what I'm doing wrong.
# Posted By Todd Rafferty | 1/23/08 7:23 PM
Cutter's Gravatar @Todd - Just a thought, but if someone has javascript turned off then chances are they don't have cookies enabled either, which then negates the 'session' experience.
# Posted By Cutter | 1/24/08 12:28 AM
Todd Rafferty's Gravatar @Cutter - In a new world with Firefox + NoScript + AdBlock? I'll take my chances. I'm more concerned about the user not having javascript enabled.
# Posted By Todd Rafferty | 1/24/08 1:13 PM
Scott Bennett's Gravatar People with JavaScript disabled are in a pretty extreme minority these days. In the analytics for the fairly popular e-commerce site I work on, over 99% of users have JavaScript enabled and the less than 1% that have it disabled are mostly people browsing from thier blackberries or other phones. So I seldom bother to do much to cater to them, snd if I were you I would probably just go with simple <noscript> tag.

That being said, if I were to build what you are describing, then in application.cfc I would set up a variable in the onSessionStart function called "session.jsenabled", with a defaul value of "NO". Then I would use my session.cfc above to set the variable to be "YES", by including the following snippet of code in any request where the session:

<cfajaxproxy cfc="session" jsclassname="CFCs.session">

<cfset AjaxOnLoad("checkJSEnabled")>

<script language="JavaScript">
checkJSEnabled = function(){
var SessionObj = new CFCs.session();
SessionObj.setErrorHandler(errorHandler);
SessionObj.SetSessionVar("JSEnabled", "YES");
}

errorHandler = function(statusCode,statusMsg) {
alert(statusCode+': '+statusMsg)
}
</script>

Then I could put some conditional logic on the necessary pages to check the session variable and let the user know if they are missing out on my cool AJAX features.

Although after writing that out I'm not sure how practical it is. If the very first page they view is an AJAX enabled page and you haven't set the value in their session to "Yes" yet, then your message telling them that javascript is needed for this page would be displayed until they refreshed. so you would have to add some more logic to that checkJSEnabled function that will hide your message if javascipt is actually enabled.

I would just use the noscript tag if I were you.
# Posted By Scott Bennett | 1/24/08 1:42 PM
Todd Rafferty's Gravatar I disagree that people with javascript are disabled are in the minorities tho, especially with FireFox + NoScript. You can enable/disable javascript easily enough. I'm using it right now, except I had to enable it to post a comment.

I didn't finish explaining my logic above. :) After I had attempted to check at least once, the code wouldn't ever run again. Easy enough to do, only need to check once and keep the "message" in their face or perhaps give them the option to dismiss the message.

I need to give this code another stab still, but I'm buried on a project at work still. When I last attempted, everything was running properly, but when the script part ended, it was retaining it's old value instead of the new one that I assigned it inside.
# Posted By Todd Rafferty | 1/24/08 1:50 PM
Scott Bennett's Gravatar Different sites attract different audiences. On all the sites I have worked on for the last 3 years or so, I have had analytics in place and my audiences have been pretty good about always having javascript enabled. If your user base has a significant population of javascript disablers in FireFox, then you definitely want to give them some consideration.
# Posted By Scott Bennett | 1/24/08 2:05 PM
Penny's Gravatar I'm wondering in your cfc, you don't need cflock when you accessing session variables? Is it safe?
# Posted By Penny | 6/18/08 12:06 PM
Scott Bennett's Gravatar @Penny,

There are two conditions that must be met in order to require the use of CFLock:

1. A shared resource is being accessed or updated.
2. There must be the possibility of a race condition resulting in a NEGATIVE outcome.

This particular sample code was developed as part of an article that demonstrated how to interact with the session scope via the new cfajaxproxy tag.

http://www.coldfusionguy.com/ColdFusion/blog/index...

Since in that example I did not feel that a race condition would cause a negative outcome, I did not need to worry about locking the session scope. If you feel that a race condition in your application would cause a negative outcome, then by all means you should use cflock.

I wrote a fairly detailed article on CFlock several months ago that you may want to read through, and I would recommend reading through the comments and checking out all the links to other resources and information that was brought up in the discussion as well.

http://www.coldfusionguy.com/ColdFusion/blog/index...
# Posted By Scott Bennett | 6/19/08 2:19 PM
Marty's Gravatar Scott,
Thanks for this. I was chasing my tail all morning trying to figure this out. Once I made sure that the ajax calls resided within the head tags, everything lit up as described. Thanks again!
# Posted By Marty | 7/17/08 10:39 AM
Tim's Gravatar hi,

I used your example but I'm getting a 404:not found, is this because a mapping issue with Ajax? can you help? thanks
# Posted By Tim | 8/12/11 1:51 PM
Coldfusion Development's Gravatar HI ! i am learner. i try to create Function but i have one query can place a hidden field control in the ASPX page(<input type="hidden" id="MyID" runat="server">). Assign value to this hidden field in the code-behind file. Access this value in your javascript like a normal HTML control.
# Posted By Coldfusion Development | 11/3/12 6:19 AM
https://www.bababorses.de/Louis-Vuitton-Damier-Ebene-Canvas-Clapton-PM-Bag-N44243-Magnolia-2361-it.html https://www.bababorses.de/Louis-Vuitton-LV-Trainer-Men-s-Sneakers-Top-Quality-15-5322-it.html https://www.bababorses.de/Celine-Small-Cabas-Bag-In-Black-Leather-it-2087 https://www.bababorses.de/Louis-Vuitton-Heel-10cm-Call-Back-Sandals-Nude-6162-it.html https://www.bababorses.de/LOUIS-VUITTON-BREA-MM-Monogram-Vernis-Leather-In-Magenta-4069-it.html https://www.bababorses.de/Louis-Vuitton-Ring-09-557-it.html https://www.bababorses.de/Louis-Vuitton-Monogram-LV-Square-Espadrilles-Slipper-Sandals-Brown-6371-it.html https://www.bababorses.de/Prada-Golden-Saffiano-Calfskin-Leather-Top-Handle-Bag-it-2956 https://www.bababorses.de/Dior-Diorissimo-Small-Bag-Black-Nappa-Leather-Silvery-Hardware-8001-it-22 https://www.bababorses.de/Louis-Vuitton-Idylle-Blossom-Charms-Necklace-Q94360-406-it.html https://www.bababorses.de/Louis-Vuitton-Color-Blossom-BB-Star-Pendant-Necklace-Red-Gold-309-it.html https://www.bababorses.de/Bvlgari-Serpenti-Original-Leather-Framed-Pochette-Sky-Blue-82121-it-1938 https://www.bababorses.de/Louis-Vuitton-Horizon-55-Trolley-Travel-Luggage-Bag-Taiga-Leather-M30331-Red-6892-it.html https://www.bababorses.de/Fendi-By-The-Way-Small-Croc-Satchel-White-it-2731 https://www.bababorses.de/Louis-Vuitton-Monogram-Canvas-and-PVC-Nano-Bag-M61114-3176-it.html https://www.bababorses.de/Louis-Vuitton-Sarah-Multicartes-Wallet-M61273-Hot-Pink-7624-it.html https://www.bababorses.de/louis-vuitton-speedy-30--Damier-Azur-Canvas-n44367-2300-it.html https://www.bababorses.de/Hermes-Birkin-35cm-cattle-skin-vein-Handbags-blue-golden-it-907 https://www.bababorses.de/Saint-Laurent-Baby-Sac-De-Jour-Bag-In-Rose-Grained-Leather-it-3322 https://www.bababorses.de/Louis-Vuitton-Twist-MM-M53531-M53532-2775-it.html https://www.bababorses.de/Louis-Vuitton-Sunglasses-133-978-it.html https://www.bababorses.de/Louis-Vuitton-Neverfull-MM-M54185-Black-2705-it.html https://www.bababorses.de/Prada-Saffiano-East-West-Medium-Tote-Bag-Nero-it-3042 https://www.bababorses.de/Louis-Vuitton-Compact-Wallet-in-Monogram-Canvas-M63041-7399-it.html https://www.bababorses.de/Prada-Mens-Leather-Pouch-3312-Black-it-3099 https://www.bababorses.de/Louis-Vuitton-Women-s-Escale-Lock-It-Flat-Mule-1A7TOX-Pink-5965-it.html https://www.bababorses.de/Fendi-Baguette-Micro-Monster-Bag-Purple-Multi-it-533 https://www.bababorses.de/Louis-Vuitton-LV-Angel-Stud-Earrings-M64293-435-it.html https://www.bababorses.de/Louis-Vuitton-Damier-Ebene-Canvas-Zippy-Wallet-Evasion-M61360-7219-it.html https://www.bababorses.de/LOUIS-VUITTON-CATOGRAM-SQUARE-SCARF-MP2266-4818-it.html https://www.bababorses.de/Louis-Vuitton-Monogram-Empreinte-Triangle-Shaped-Messenger-Bag-M54330-Black-3865-it.html https://www.bababorses.de/Balenciaga-Velo-Anthracite-store-it-1723 https://www.bababorses.de/Chloe-Marcie-Medium-Satchel-Bag-Cobalt-it-2283 https://www.bababorses.de/louis-vuitton-epi-leather-Soufflot-BB-bag-m55613-black-2580-it.html https://www.bababorses.de/Louis-Vuitton-Dauphine-MM-M55735-4512-it.html https://www.bababorses.de/Louis-Vuitton-Crafty-NeoNoe-MM-bag-black-M45497-2980-it.html https://www.bababorses.de/Louis-Vuitton-Men-Box-Bag-Shoulder-Body-Bag-M44157-Brown-3136-it.html https://www.bababorses.de/Prada-Saffiano-Double-Zip-Executive-Tote-Bag-Gray-it-3025 https://www.bababorses.de/Louis-Vuitton-Epi-Leather-NeoNoe-BB-Bucket-Bag-M53610-Indigo-2564-it.html https://www.bababorses.de/Saint-Laurent-Small-Monogram-Tassel-Satchel-In-Red-Crocodile-Leather-it-3158 https://www.bababorses.de/Louis-Vuitton-Iphone-Case-LV18-59-it.html https://www.bababorses.de/LOUIS-VUITTON--CLASSIC-MINI-PACKBACK-2872-it.html https://www.bababorses.de/Balenciaga-Velo-Anthracite-store-it-1723 https://www.bababorses.de/Louis-Vuitton-Monogram-Ebene-Canvas-Pegase-Legere-53-Business-Rolling-Luggage-6950-it.html https://www.bababorses.de/Louis-Vuitton-Crocodilien-Brillant-Capucines-Mini-Bag-N93429-Black-2231-it.html https://www.bababorses.de/Louis-Vuitton-Monogram-Hoodie-Jacket-Black-1526-it.html https://www.bababorses.de/Louis-Vuitton-Monogram-Coated-Canvas-Popincourt-PM-M43462--Raisin-3345-it.html https://www.bababorses.de/Louis-Vuitton-Lockme-Cabas-Tote-M55028-Black-4530-it.html https://www.bababorses.de/Givenchy-Antigona-Small-Leather-Satchel-Bag-Black-it-2432 https://www.bababorses.de/Louis-Vuitton-Monogram-Canvas-Small-Malle-Chain-Bag-3294-it.html https://www.bababorses.de/Louis-Vuitton-Epi-Leather-Zippy-Wallet-M62304-Red-7304-it.html https://www.bababorses.de/Louis-Vuitton-Iphone-Case-LV113-38-it.html https://www.bababorses.de/Louis-Vuitton-Heel-10.5cm-Eyeline-Pumps-Python-Pattern-Suede-Black-5999-it.html https://www.bababorses.de/LOUIS-VUITTON-PEGASE-LEGERE-REGATTA-N41620-MONOGRAM-CANVAS-6974-it.html https://www.bababorses.de/Louis-Vuitton-Kimono-Wallet-M56175-Pink-7437-it.html https://www.bababorses.de/Louis-Vuitton-Monogram-Tapestry-Denim-Bidart-Espadrilles-Blue-5726-it.html https://www.bababorses.de/Louis-Vuitton-Women-s-Escale-Shirtdress-Blue-1709-it.html https://www.bababorses.de/Louis-Vuitton-Montaigne-MM-M41048-Black-4597-it.html https://www.bababorses.de/Louis-Vuitton-Heel-10cm-Crystals-Call-Back-Sandals-Suede-Red-6160-it.html https://www.bababorses.de/Hermes-Bolide-31cm-Togo-Leather-Green-Bag-it-1070 https://www.bababorses.de/Replica-Hermes-Wallet-H001-Wallet-Cow-Leather-Green-it-1558 https://www.bababorses.de/Celine-Medium-Luggage-Tote-Black-Brown-White-Bag-it-2168 https://www.bababorses.de/Louis-Vuitton-Pochette-Voyage-MM-Bag-Damier-Graphite-Canvas-Pixel-N60176-Green-7278-it.html https://www.bababorses.de/Fendi-Black-Snake-Veins-Leather-With-Beige-Ferrari-Leather-Top-handle-Bag-it-469 https://www.bababorses.de/Louis-Vuitton-All-over-Monogram-Sleeveless-Belted-Dress-Navy-1375-it.html https://www.bababorses.de/Louis-Vuitton-Ring-02-560-it.html https://www.bababorses.de/Louis-Vuitton-Iphone-Case-LV32-76-it.html https://www.bababorses.de/Louis-Vuitton-Dauphine-MM-M55071-Blue-4511-it.html https://www.bababorses.de/Louis-Vuitton-Supreme-Iphone-Case-White-Red-212-it.html https://www.bababorses.de/Louis-Vuitton-Epi-Smooth-Leather-Twist-Shoulder-Bag-MM-Pink-Black-2639-it.html https://www.bababorses.de/Louis-Vuitton-Monogram-Empreinte-Leather-Cosmetic-Pouch-Bag-M80502-Bouton-de-Rose-Pink-By-The-Pool-Capsule-Collection-4296-it.html https://www.bababorses.de/Louis-Vuitton-Bracelet-21-271-it.html https://www.bababorses.de/Louis-Vuitton-Heel-9.5-cm-Star-Trail-Ankle-Boots-Black-5511-it.html https://www.bababorses.de/Louis-Vuitton-Geronimos-Belt-Bag-M43502-Black-Epi-Leather-2646-it.html https://www.bababorses.de/Louis-Vuitton-Damier-Ebene-Canvas-Vavin-Chain-Wallet-N60222-Bordeaux-Red-7221-it.html https://www.bababorses.de/Louis-Vuitton-Monogram-Empreinte-Leather-Zippy-Coin-Purse-M80408-Cream-Saffron-By-The-Pool-Capsule-Collection-7741-it.html https://www.bababorses.de/Louis-Vuitton-Vintage-Monogram-Vernis-Bleecker-Box-Top-Handle-Bag-Burgundy-4172-it.html https://www.bababorses.de/Prada-Saffiano-Mini-Galleria-Crossbody-Bag-Beige-it-2708 https://www.bababorses.de/Louis-Vuitton-Sunglasses-39-1042-it.html https://www.bababorses.de/Louis-Vuitton-Monogram-Canvas-Leopard-Print-Onthego-Tote-Bag-M44674-Black-White-3232-it.html https://www.bababorses.de/Louis-Vuitton-Epi-Leather-Twist-PM-Bag-with-Crystal-embellished-Chain-M55412-White-2630-it.html https://www.bababorses.de/Fendi-Chameleon-Red-Cross-Veins-Leather-Tote-Bag-it-488 https://www.bababorses.de/Louis-Vuitton-Monogram-Canvas-Onthego-Tote-Bag-M44571-Kaki-3270-it.html https://www.bababorses.de/Fendi-Earth-Yellow-Leather-with-Multicolor-Striped-Fabric-Shopping-Handbag-it-771 https://www.bababorses.de/Louis-Vuitton-Croco-Pattern-Petite-Boite-Chapeau-Bag-Black-4090-it.html https://www.bababorses.de/Prada-Saffiano-Small-Double-Handle-Tote-Bag-Light-Gray-Pomice-it-2849 https://www.bababorses.de/Louis-Vuitton-Lvxlol-Speedy-BB-M45202-Golden-3125-it.html https://www.bababorses.de/Louis-Vuitton-Gloria-Flat-Open-Back-Loafers-Monogram-Canvas-5798-it.html https://www.bababorses.de/LOUIS-VUITTON-BREA-PM-Monogram-Vernis-leather-IN-MORDORE-4074-it.html https://www.bababorses.de/Replica-Hermes-Steve-H2810-Ladies-Shoulder-Bag-Cow-Leather-it-1428 https://www.bababorses.de/Louis-Vuitton-Noe-bag-M42226-Brown-3496-it.html https://www.bababorses.de/Louis-Vuitton-Damier-Azur-Canvas-I-2260-it.html https://www.bababorses.de/Christian-Dior-Multicolor-PeachYellow-Zipper-Wallet-118-it-223 https://www.bababorses.de/Fendi-By-the-Way-Small-Tricolor-Satchel-Bag-it-2916 https://www.bababorses.de/Prada-Medium-Vitello-Diano-Open-Tote-Bag-Pomice-it-2728 https://www.bababorses.de/Luxury-Hermes-Wallet-H001-Unisex-Wallet-it-1598 https://www.bababorses.de/Louis-Vuitton-Iphone-Case-LV42-104-it.html https://www.bababorses.de/Prada-Saffiano-Small-Gardeners-Tote-Bag-Blue-it-2803 https://www.bababorses.de/Louis-Vuitton-Sac-Tricot-Bag-Epi-Leather-Red-M52805-2736-it.html https://www.bababorses.de/Louis-Vuitton-Crazy-in-Lock-Strass-Bracelet-Silver-316-it.html