Escape characters between ruby and javascript
-
i have a javascript like this:
settings=x+","+y+","+z; query = 'skp;_edit@'+settings; window.location.href = query;
in ruby:
window.add_action_callback("_edit") {|window,p| resultarray=p.split(",")
Now the x, y or z value in javascript can contain commas.
So this will cause problems for the resultarray in ruby.
What is the best way to escape the comma's (or any problem characters in javascript) so they are comming to ruby in a usable way?Off course the other way arround is also a problem:
rubyrubyarray=['blablabla,','boem,boem,boem','world'] #join the array to send to javascript rubyarray=rubyarray.compact.join(",") command = "_test('#{rubyarray}')" window.execute_script(command)
In javascriptI split up the string by comma
function _test(x) p = x.split(',');
This again gives me to much elements. How do i escape the unneccesary comma's in the ruby string?
Thx!
-
@pout said:
i have a javascript like this:
settings=x+","+y+","+z; > query = 'skp;_edit@'+settings; > window.location.href = query;
in ruby:
window.add_action_callback("_edit") {|window,p| > resultarray=p.split(",")
Now the x, y or z value in javascript can contain commas.
So this will cause problems for the resultarray in ruby.What is the best way to escape the comma's (or any problem characters in javascript) so they are comming to ruby in a usable way?
If you know that you're going to have commas in the variable values then a comma as a separation character is probably not a good choice. That said, you could first use javascript's escape() on all your variables which will turn every "," in a "%2c" and then join them with your separating ",".
On the Ruby side you can find a "URL-decode" functions in the CGI module:
def urlDecode(string) ## URL-decode from Ruby;;CGI string.tr('+', ' ').gsub(/((?;%[0-9a-fA-F]{2})+)/n) do [$1.delete('%')].pack('H*') end end
@pout said:
Off course the other way arround is also a problem:
rubyrubyarray=['blablabla,','boem,boem,boem','world'] > #join the array to send to javascript > rubyarray=rubyarray.compact.join(",") > command = "_test('#{rubyarray}')" > window.execute_script(command)
In javascriptI split up the string by comma
function _test(x) > p = x.split(',');
Same procedure only the other way around. These days I prefer to build a JSON string in Ruby and eval() that on the javascript side for anything that's a bit more complex. In your case that would take car of the the encoding because your rubyarray also works as javascript array.
Cheers,
Thomas -
Or, you could make 3 callbacks instead, one for x, another for y and a third for z.
-
Another option would be prefix the length of each "section" in front of the section in javascript, like this: (adding a colon to separate the length from the value)
var settings = '' ; var x = 'abc' ; var y = 'd,e,f' ; var z = '1,2345' ; settings = settings + x.length + ';' + x ; settings = settings + y.length + ';' + y ; settings = settings + z.length + ';' + z ;
This yields:
3;abc5;d,e,f6;1,2345
-
Thx Thomas and Tod.
Tod,
What would be the most correct way then to split the settings string again in ruby?I had another idea, dunno if it is a descent one:
If i replace every possible problem character with a code eg.
,--><c0mm4>
'--><qu0t3>
"--><2qu0t3>And this at both the javascript as the ruby side (and re-encode them if necessary).
Chances of having that exact value in a string are close to nil I think.Would this be a good and solid solution, or just a workarround for dummies?
-
Thomas,
When I use your suggestion it does not seem to work
escape in javascript indeed give me %c2
but the p value in the callback still contains , in the stringsexample
x="abc"; y="1,2,3"; z="1,23"; x=escape(x); y=escape(y); z=escape(z); settings=x+","+y+","+z; alert (settings); -->abc,1%c22%c23,1%c223 query = 'skp;_edit@'+settings; window.location.href = query;
window.add_action_callback("_edit") {|window,p| puts p -->abc,1,2,3,1,223
-
Here's a link that shows how to convert from ascii to hex in javascript. If you convert the whole string to hex, you can use the pack() method in Ruby to get it back to ascii. http://www.ajaxblender.com/howto-convert-ascii-hex-binary-decimal.html.
-
Thx all. I managed to get it working
Advertisement