Execute_script trouble
-
Hello, I am starting to get the hang of the webdialogs when being used to makes changes in Sketchup, however I have hit a snag while attempting to use the execute_script.
How do I format the information I am trying to pass to JS through the a functions parameters. In my simple test of just trying to pass a string in the parameters and having it printed in the HTML file i keep getting an error message stating: 'test' is undefined, sounds to me like its not taking its value as a string...or I'm not formating it right..or defining it? (from what i have looked at I haven't seen a need to define it)
Ruby (this is inside an action_callback that works fine)
updated_visions_div = "test" js_update_command = "UpDateVisionList("+ updated_visions_div +")" web_dialog.execute_script(js_update_command)
JavaScript
function UpDateVisionList(incoming) { document.getElementById("field_of_visions").innerHTML = incoming; }
Once I hit the button that triggers the command a scripting error in the webdialog appears telling me "test" is undefined
I have played with a few different ways, but from what I have read I don't see why this is not working. Most likely I am missing something obvious! 9 time out of 10 thats the case anyways. Could someone please educate me on whats up?
Cheers~
Korbin -
Try making
js_update_command = "UpDateVisionList("+ updated_visions_div +")"
as
js_update_command = "UpDateVisionList(\""+updated_visions_div+"\")"
-
Not sure if this is what is tripping you up, but I usually use a ";" at the end of my javascript command. JS is pretty picky about semi colons:
js_update_command = "UpDateVisionList("+ updated_visions_div +");"
-
@tig said:
Try making
js_update_command = "UpDateVisionList("+ updated_visions_div +")"
as
js_update_command = "UpDateVisionList(\""+updated_visions_div+"\")"
awesome! that totally worked, and i know i wouldn't of thought to do that myself.
So do those escapes keep the quotations around the word so JS knows its a string and not some undeclared variable?
Thanks a ton TIG
(It worked without adding the ;, but thanks for the response)
-
OT: speaking of semicolons - they are not needed unless a very specific case - see http://mislav.uniqpath.com/2010/05/semicolons/
-
@kdasilva said:
So do those escapes keep the quotations around the word so JS knows its a string and not some undeclared variable?
Yes. The string you send to execute_script is processed by JS
eval()
in the webdialog. So remember that all strings you send to execute_script is processed and evaluated twice, once in Ruby and once in JS.Though, the code will be easier to re4ad and understand if you avoid escaping quotes.
js_update_command = "UpDateVisionList('#{updated_visions_div}')"
Advertisement