Web Dialog: Trouble passing string from ruby to javascript
-
“var” is a String type ruby variable. I can’t pass characters to web dialog box though the ruby variable “var”. This works fine when the string contains integers. (eg: var=”12”).I can’t figure out why?
I like to know about your suggestions about this problem.Ruby Code
` var= "a"
dialog = UI::WebDialog.new("Building details", true, "", 410, 875, 1030, 0, true)dialog.add_action_callback("pass_data"){|dialog,htmlpage|
java = "set_id("+var+")"
dialog.execute_script(java)
}`%(#0000BF)[Html and javascript
<html>
<script>
function callRuby(htmlpage) {
query = 'skp:pass_data@' + htmlpage;
window.location.href = query;}function set_id(var){
var message = "ID = " +var+ " ";
document.getElementById('output').innerHTML = message;}
</script><body>
<h3 id="output">Object ID </h3>
<input type="button" onclick="callRuby('pull_selection_count')" value="Refresh">
</body>
</html>] -
You must ensure to compile a proper JavaScript string - check the string you send to
execute_script
.From your example:
<span class="syntaxdefault"></span><span class="syntaxkeyword">var=</span><span class="syntaxdefault"> </span><span class="syntaxstring">"a"<br /></span><span class="syntaxdefault">java </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">"set_id("</span><span class="syntaxkeyword">+var+</span><span class="syntaxstring">")"<br /></span><span class="syntaxdefault">dialog</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">execute_script</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">java</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"></span>
The string
java
you send to the WebDialog is compiled to:
set_id(a)
You see your problem now? You're asking of a JS variable
a
instead of a string"a"
.That's why a number works:
<span class="syntaxdefault"></span><span class="syntaxkeyword">var=</span><span class="syntaxdefault"> </span><span class="syntaxstring">"5"<br /></span><span class="syntaxdefault">java </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">"set_id("</span><span class="syntaxkeyword">+var+</span><span class="syntaxstring">")"<br /></span><span class="syntaxdefault">dialog</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">execute_script</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">java</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"></span>
This compiles to:
set_id(5)
- which is valid.Because the string you send to the webdialog is a javascript source string you must ensure the correct quotes are there.
A quick way to ensure strings are properly escaped is using the
.inspect
method.<span class="syntaxdefault"></span><span class="syntaxkeyword">var=</span><span class="syntaxdefault"> </span><span class="syntaxstring">"a"<br /></span><span class="syntaxdefault">java </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">"set_id(#{var.inspect})"<br /></span><span class="syntaxdefault">dialog</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">execute_script</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">java</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"></span>
This will translate to
set_id("a")
Further more, if you have single and double quotes that needs to be there, they will be properly escaped as well:
<span class="syntaxdefault"></span><span class="syntaxkeyword">var=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">%{</span><span class="syntaxdefault">A string with </span><span class="syntaxstring">"double"</span><span class="syntaxdefault"> and </span><span class="syntaxstring">'single'</span><span class="syntaxdefault"> quotes</span><span class="syntaxkeyword">.}<br /></span><span class="syntaxdefault">java </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">"set_id(#{var.inspect})"<br /></span><span class="syntaxdefault">dialog</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">execute_script</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">java</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"></span>
This will translate into:
set_id("A string with \"double\" and 'single' quotes.")
-
Note, I also used string interpolation:
#{}
inside the string - it's much faster way to compile strings than usingstring + another_string
. I also find it read easier as well. -
Thank you very much
it works just fine
Advertisement