Do not show content in my WebDialog when execute_script


#1

I create a WebDialog to show the coordinates of point. It does not work. But when I add a statement “UI.message”, the coordinates of point was shown in the WebDialog after click OK of the message box. I am confused. The Code is below:

dlg = UI::WebDialog.new("showPoints", true, "ShowCpoint", 400, 250, 150, 150, true)
html = <<HTML_CONTENT
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta http-equiv = "X-UA-Compatible" content = "IE=9,chrome=1"/>
   <script type="text/javascript">
    
    function showCpoint(txt){
      document.getElementById("id").innerHTML += txt +"<br/>"
      document.getElementById("id").scrollTop(9999);
    }

  </script>
 </head>
 <body>
   <div id ="id"></div>
 </body>
</html>
HTML_CONTENT
dlg.set_html(html)
dlg.show

point = Geom::Point3d.new(10,10,10)
UI.messagebox("Hi!")

arg = "'" +  point.x.to_s + "," + point.y.to_s + "'"
dlg.execute_script("showCpoint" + "(" + arg + ")")

#2

put three backticks (```) on a line before and a line after your code and that will turn off the site’s processing of your code as Markdown format.


#4

Thank you.


#5

The webdialog needs time to open, and there is not yet any HTML immediately after you do show.
The method UI::WebDialog#show is asynchronous, meaning it returns to Ruby (and continues with the next line) before it is finished. In contrast, UI.messagebox is synchronous and returns only after you have clicked “OK”. By then, the webdialog was ready and could display the coordinates.

To work with asynchronous methods, one usually uses callbacks. That means when the HTML is ready (DOMContentLoaded), you notify (with JavaScript) your Ruby code and run a callback that you have registered before.

The old UI::WebDialog (not UI::HTMLDialog) allows to register the callback directly with the show method as a Ruby code block:

point = Geom::Point3d.new(10,10,10)
dlg.show{
  arg = "'" +  point.x.to_s + "," + point.y.to_s + "'"
  dlg.execute_script("showCpoint" + "(" + arg + ")")
}

#6

It can work now.Thank you, Aerilius.


#7

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.