Example html dialog (suite)

Nope - not interested - I’ve used my solutions since the early 80’s - no issues.

Why change something that isn’t broke (obviously rhetorical)

Boy, not easy to find pertinent example code.

https://www.w3schools.com/js/js_json.asp
http://ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html

1 Like

:woman_technologist:

1 Like

Yes, saw that. Thanks
Now struggling with read and write from local file.
Tried with Fetch. but of course, I got a Cross origin error.

See:


Search results for “JSON” in Ruby API category

I do think this has to be underlined once more. !!

I’m just trying to get the Html dialog to show the last used values.

rb side
read values from local file √
make a hash with them √
hash.to_json √ json_str => {“thi”:“150”,“hei”:“2222”}.

Something is wrong in the following:

dialog.execute_script(“var jvals = #{json_str};”)

HTML side:
in a script

var Vals = JSON.parse(jvals)
document.getElementById(“thi”).value = Vals.thi
document.getElementById(‘hei’).value = Vals.hei

I’m not an expert, but maybe you need quotes around the interpolated json_str when you pass it to jscript? like

dialog.execute_script(“var jvals = “#{json_str}””)

Strings literals says it will get evaluated if the whole string is in “”

The irony here Steve, is that on Mac, I can just use an array. Pass it to html with a text sub and there it is seen as an array. Works pretty fine.
But not on pc.

I run the following - works on both PC and Mac where data is a string.

script = "from_ruby_( '" << data << "' )"
dialog.execute_script(script)

What if data is a string that contains ' ?

2 Likes

Mine do not - I replace all single quotes with back quotes.

@mariocha You do not really need the JS var jvals. Try …

dialog.execute_script("var Vals = JSON.parse('#{json_str}');")

The outer double quote is for Ruby interpolation.
The single quote around the JSON string is for JavaScript because JSON string data itself uses double quotes.

But you can also do …

dialog.execute_script("var jvals = '#{json_str}';")
2 Likes

Thanks a lot Dan, I really needed a confirmation of one of my many tries.
working on the HTML side now.
Do I need to JSON.parse there ?
does the line. var jvals = ‘#{json_str}’; has to also be in the html ?

Yep, that happens a lot in the imperial world.

And that is precisely why I replace single quotes with back quotes.

Thanks Dan! That’s exactly what I was trying to say.

What if someone needs to express a length in imperical units? A back quote is not the same as the ’ symbol.

1 Like

Where we are back at escaping, not replacing.

1 Like