No Clean Feed - Stop Internet Censorship in Australia

Entries Tagged as 'Javascript'

cfajaxproxy exposing CFC path in page source

[Edit: The following post has been edited/re-worded in an attempt to clear up it's meaning]

I've been playing around with ColdFusion's cfaxajproxy and loving it. I was interested however to find that when the page is rendered in the browser, cfajaxproxy outputs (in the source code) the full path [Edit: literal relative path] to the CFC I'm having it call.

Example:

Source output (Javascript):
var _cf_Expertise=ColdFusion.AjaxProxy.init('/x/y/z/Expertise.cfc','expertiseCFC');

I tried using a Mapping in the CF Admin to the directory (under my webroot) containing my CFCs but still the full path [Edit: literal relative path] from root to my CFC is exposed.

Example:
Mapping made in CF Admin
"/mapping" -> "c:/.../x/y/z/"

Source output (Javascript):
var _cf_Expertise=ColdFusion.AjaxProxy.init('/x/y/z/Expertise.cfc','expertiseCFC');

When I created a Mapping to a folder *above* the webroot containing my CFC the source output changed to include the mapping reference (this is the behaviour/output I wanted, just without having to have the folder containing the CFC above the webroot).

Example:
Mapping made in CF Admin
"/mapping" -> "c:/.../{folder above web root}/"

Source output (Javascript):
var _cf_Expertise=ColdFusion.AjaxProxy.init('/mapping/Expertise.cfc','expertiseCFC');

Why is it that (using the CF Admin mapped path) when the CFC folder is above the webroot cfaxajproxy outputs the shorter mapped reference to the Javascript source code but when the CFC folder is under the webroot the full relative path from the webroot to the CFC is exposed in the Javascript source code? Is there a way around this so that the mapping is always output (I need my CFCs within the webroot, not above)?

[Edit: Additional Info]

This is how I am calling cfajaxproxy:

<cfajaxproxy
cfc = "mapping.Expertise"
jsclassname = "expertiseCFC" />

Where "mapping" is the virtual Mapping reference set within CF Admin.

Subscribe

Javascript line break rendering confusion

This is something that I know I will forget each time I run into it, so it's going up here for reference!

I use Javascript a fair bit, not as an advanced user but for everyday things like validation and moderate UI dynamic modifications. I was recently returning a string value from a remote ColdFusion function directly to a Javascript callback function then displaying it via alert() to the user. The string I returned contained "\n" characters in it so that I could control where the line breaks were to be when the alert function displayed it in a pop-up. What I wasn't counting on was that javascript would ignore these line breaks and simply display the "\n" string along with the rest of the message in one clump of characters all on a single line!

I figure, and please correct me if I am wrong, that since the string was created by ColdFusion as just that, a string, Javascript is not recognising the line breaks. If however, I was to create the exact same string in Javascript containing the same "\n" characters, it would indeed render correctly placing the line breaks where they have been indicated. Javascript seems to escape the \n values in the string passed by ColdFusion (like so: "\\n") and therefore negates the line break values.

Example:

ColdFusion returns the string: "Line one\nLine two\nLine three" and I would expect this to appear in an alert popup box as:

Line one
Line two
Line three

However the Javascript alert instead displays it as:

Line one\nLine two\nLine three

To fix this I had to use a Javascript global replace fucntion on the escaped characters in the string:

var newString = returnedString.replace(/\\n/g, '\n'); alert(newString);

Note that you have to use the "g" value in the above replace line. It stands for global - this way you replace all instances in the string and not just the first.

And that's that.

 

Subscribe

TweetTicker: read Twitter Atom/RSS with NewsTicker and ColdFusion

TweetTicker The other day at work I decided see what could be done with Twitter Atom/RSS feeds. I wanted to be able to have a scrolling vertical 'ticker' of Tweets, but without having to use Twitter's developer API.

I did a quick Google search to see what pre-built tools I might be able to pull together and use  and came across NewsTicker, a Javascript tool based on the MooTools Javascript framework. It was being used for News items, hence the name, but had the same functionality I had been looking for to display Tweet items.

So, using NewsTicker and ColdFusion's <CFFEED/> tag to easily grab my Twitter feed I was quickly and easily able to put something together in no time.

To make sure this post does not go into too unnecessary detail, I've provided a link to both a demo and and ZIP archive at the bottom of this post incase anyone would like to modify it for their own needs. My example is of course fairly basic and simply an experiment so I'm sure you can make it more advanced without any trouble :-).

Some things worth noting:

Since I am in Australia, I had to convert the date/time values retrieved from the Atom/RSS feed to a relative time reference based on my time zone.

<!--- Set Locale: --->
<cfset setLocale("English (Australian)") />
<!--- Apply timezone offset of hours: --->
<cfset pubDateTime = DateAdd("h",-GetTimeZoneInfo().UTCHourOffset,pubDateTime) />

I then needed to format my date/time value to show a value relative to 'now'. I used Ray Camden's Relative Time UDF:

<cfscript>
function relativeTime(pastdate) {
var delta = dateDiff("s", pastDate, now());
if(delta < 60) {
return "less than a minute ago";
} else if(delta < 120) {
return "about a minute ago";
} else if(delta < (45*60)) {
return round(delta/60) & " minutes ago";
} else if(delta < (90*60)) {
return "about an hour ago";
} else if(delta < (24*60*60)) {
return round(delta/3600) & " hours ago";
} else if(delta < (48*60*60)) {
return "1 day ago";
} else {
return round(delta/86400) & " days ago";
}
}
</cfscript>

To get the feed from Twitter I used:

<!--- Default the Twitter user you would like to use: --->
<cfparam name="url.twitterUser" default="nothingwithyou" />
<!--- Set Twitter Atom/RSS feed URL: --->
<cfset feedurl="http://search.twitter.com/search.atom?q=#url.twitterUser#" />
<!--- Get Twitter RSS feed for the intended user: --->
<cffeed
source="#feedurl#"
properties="feedmeta"
query="feeditems" />

It is quite easy to choose either the Atom or RSS feed to read, by simple changing part of the URL string from "search.atom" to "search.rss".

Anyway, check out the demo and source and see how it goes. When viewing the demo, if you have a Twitter account or know of one you like, try adding:

?twitterUser={twitter_account_name_here}

to see the reader pick up that account's feed.

View Demo | Download Source

Note: I am running ColdFusion 8

Note: Functionality for refreshing Tweet items in the Demo and Source Code has been removed.

Edit:

Additional Twitter API search examples (ensure URL encoding):

  • Containing a word: http://search.twitter.com/search.atom?q=twitter
  • From a user: http://search.twitter.com/search.atom?q=from%3Aal3x
  • Replying to a user (tweet starts with @mzsanford): http://search.twitter.com/search.atom?q=to%3Amzsanford
  • Mentioning a user (tweet contains @biz): http://search.twitter.com/search.atom?q=%40biz
  • Containing a hashtag (up to 16 characters): http://search.twitter.com/search.atom?q=%23haiku
  • Combine any of the operators together: http://search.twitter.com/search.atom?phrase=happy+hour&until=2009-03-24
  • Originating from an application: http://search.twitter.com/search.atom?q=landing+source:tweetie

There is so much you can do via the API's search function (additional parameters, boolean operators, Curl/ JSON response formating), check it out here: Twitter API Wiki: Search Method

Subscribe