5 Replies - 1311 Views - Last Post: 09 October 2013 - 09:00 AM Rate Topic: -----

#1 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 193
  • Joined: 03-December 08

Character used as javascript separator. What is it?

Posted 09 October 2013 - 08:16 AM

Hello and thanks for youre time. I have a quick question regarding a strange character used as a separator in javascript.

Coldfusion returned to an ajax block:
<cfoutput>:-:</cfoutput>
<cfoutput>#session.themeColor#</cfoutput>
<cfoutput>:-:</cfoutput>



Javascript which consumes the callback (partial):
success: function(results){
var resultsArr = results.split(":-:");
resultsArr.pop();
}



Let's say the #session.themeColor# holds the value of "light".

I dumped resultsArr and got ",light,"

After the pop(), the last item in the array was removed and I got ",light"

So the ":-:" resolves to a comma??? I've never seen this before, so I'm just looking to learn about this one.

Any guidance will be greatly appreciated as always!

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Character used as javascript separator. What is it?

#2 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 193
  • Joined: 03-December 08

Re: Character used as javascript separator. What is it?

Posted 09 October 2013 - 08:20 AM

Hello and thanks for youre time. I have a quick question regarding a strange character used as a separator in javascript.

Coldfusion returned to an ajax block:
<cfoutput>:-:</cfoutput>
<cfoutput>#session.themeColor#</cfoutput>
<cfoutput>:-:</cfoutput>



Javascript which consumes the callback (partial):
success: function(results){
var resultsArr = results.split(":-:");
resultsArr.pop();
}



Let's say the #session.themeColor# holds the value of "light".

I dumped resultsArr and got ",light,"

After the pop(), the last item in the array was removed and I got ",light"

So the ":-:" resolves to a comma??? I've never seen this before, so I'm just looking to learn about this one.

Any guidance will be greatly appreciated as always!

Thanks!
Was This Post Helpful? 0
  • +
  • -

#3 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1167
  • View blog
  • Posts: 7,208
  • Joined: 07-September 06

Re: Character used as javascript separator. What is it?

Posted 09 October 2013 - 08:21 AM

My guess is that the exact output is (based on your example) something like so:
:-:light:-:, at which point you calling split on it splits the string every time it finds the separator :-:.

So, you are getting an array of an empty element, "light", and another empty element from the split call. Then you just need to go through the array, remove all the empty elements, and you will be left with all the elements that actually have some content.

Hope that makes sense.
Was This Post Helpful? 0
  • +
  • -

#4 Craig328  Icon User is offline

  • I make this look good
  • member icon

Reputation: 1943
  • View blog
  • Posts: 3,481
  • Joined: 13-January 08

Re: Character used as javascript separator. What is it?

Posted 09 October 2013 - 08:23 AM

Hm. If it evaluated to a comma then you'd think you should be seeing ",light,".

My guess is that it sees the first CFOUTPUT result is being read as a null result which would be why "light" is preceded by the comma (as though there was a value in front of it).
Was This Post Helpful? 0
  • +
  • -

#5 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 193
  • Joined: 03-December 08

Re: Character used as javascript separator. What is it?

Posted 09 October 2013 - 08:41 AM

View PostCraig328, on 09 October 2013 - 09:23 AM, said:

Hm. If it evaluated to a comma then you'd think you should be seeing ",light,".


I do get ",light," when I alert the resultsArr. Then after the pop I alert again and get ",light". Then the code proceeds to trim the first character (the remaining comma).

$.trim(resultsArr[0])



Why all the trouble? Why not just return "light" in the first place??? This is legacy code by the way.

Thanks!
Was This Post Helpful? 0
  • +
  • -

#6 Craig328  Icon User is offline

  • I make this look good
  • member icon

Reputation: 1943
  • View blog
  • Posts: 3,481
  • Joined: 13-January 08

Re: Character used as javascript separator. What is it?

Posted 09 October 2013 - 09:00 AM

My guess would be because there's no element following "light" so it doesn't evaluate the array (list) as having an element that comes after it so no need to insert the trailing comma.

That's entirely a guess tho. I really don't know.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1