@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:
ruby
rubyarray=['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