# Ruby callbacks not called from javascript

**URL:** <https://forums.sketchup.com/t/ruby-callbacks-not-called-from-javascript/60719>\
**Category:** Ruby API\
**Created:** [January 29, 2018, 10:53am UTC](https://forums.sketchup.com/t/ruby-callbacks-not-called-from-javascript/60719 "2018-01-29T10:53:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rajawat](https://avatars.discourse-cdn.com/v4/letter/r/ecd19e/32.png) [@rajawat](https://forums.sketchup.com/u/rajawat)\
**Post date:** [January 29, 2018, 10:53am UTC](https://forums.sketchup.com/t/ruby-callbacks-not-called-from-javascript/60719/1 "2018-01-29T10:53:20Z")

</div>

Hi All,  
I create a dialog and save its instance for showing/hiding.  
When I show it first everything works fine. If I close the dialog and show it again then sketchup methods are not called. Here is the test code

```
  d = UI::HtmlDialog.new()
      d.set_html(%[<!DOCTYPE html>
<html>
   <head>
      <link rel="stylesheet" href="./../css/style.css" type="text/css" />
      <title>Test</title>
   </head>
   <body>
      <script>
        window.onload = function() {
          sketchup.onloaded();
        }
      </script>
   </body>
</html>
])
      d.add_action_callback("onloaded") do |wd|
        puts "On loaded called from js"
        d.close
        d.show
      end
      d.show {
        puts "Show the dialog"
      }

```

I know it is recursive but it is not behaving as it should.  
Javascript error : sketchup.onloaded is not a function (sketchup instance exists)  
What I am doing wrong here ? or I have to create new instance of dialog everytime ?

---

<div class="post-metadata">

**Author:** ![john\_drivenupthewall](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@john\_drivenupthewall](https://forums.sketchup.com/u/john_drivenupthewall)\
**Post date:** [January 29, 2018, 3:00pm UTC](https://forums.sketchup.com/t/ruby-callbacks-not-called-from-javascript/60719/2 "2018-01-29T15:00:02Z")

</div>

> [@rajawat](#):
>
> What I am doing wrong here ?

nothing, it was broken in an attempt to stop CEF crashing SU during application close…

the crashes were deemed to be caused when javascript was left ‘open’ in CEF by extensions, but why it needed to clear the ruby callbacks is a mystery…

on a mac it also uses a different window ‘style’ when opened more than once…

> [@rajawat](#):
>
> or I have to create new instance of dialog everytime ?

you don’t want to make additional `UI::HtmlDialog.new()`…

but you do need to re-add the call backs

```ruby
@d = UI::HtmlDialog.new()
@d.set_html(%[<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="./../css/style.css" type="text/css" />
<title>Test</title>
</head>
<body>
Message
<script>
  window.onload = function() {
    sketchup.onloaded();
  }
</script>
</body>
</html>
])

def dlg
  # add the callback each time...
  @d.add_action_callback("onloaded") do |wd|
    puts "On loaded called from js from #{@d}"
    @d.close
  end
  # this happens first
  @d.show 
  # #show{} this only works with WebDialogs
  # not HtmlDialogs, so put 'puts' on new line
  puts "Show the dialog"
end

```

john

---

<div class="post-metadata">

**Author:** ![rajawat](https://avatars.discourse-cdn.com/v4/letter/r/ecd19e/32.png) [@rajawat](https://forums.sketchup.com/u/rajawat)\
**Post date:** [January 29, 2018, 5:29pm UTC](https://forums.sketchup.com/t/ruby-callbacks-not-called-from-javascript/60719/3 "2018-01-29T17:29:27Z")

</div>

If you call dlg multiple times onloaded callback is not called everytime.

Although it works for WebDialog but I do not want to switch WebDialog as it is deprecated.

---

<div class="post-metadata">

**Author:** ![tt\_su](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tt_su/32/17_2.png) [@tt\_su](https://forums.sketchup.com/u/tt_su)\
**Post date:** [January 30, 2018, 1:13pm UTC](https://forums.sketchup.com/t/ruby-callbacks-not-called-from-javascript/60719/4 "2018-01-30T13:13:11Z")

</div>

The behaviour of HtmlDialog where you need to re-register action callbacks is described in our HtmlDialog examples. (Though we should update the docs to make this clearer.)

The examples can be found here: [GitHub - SketchUp/htmldialog-examples: Examples of using SketchUp Ruby API's UI::HtmlDialog class](https://github.com/SketchUp/htmldialog-examples)

Example 02 demonstrate how the action callbacks are lost between close/reopen. Example 03 demonstrate how to make it reusable: [htmldialog-examples/step03.rb at main · SketchUp/htmldialog-examples · GitHub](https://github.com/SketchUp/htmldialog-examples/blob/master/src/step03.rb)

---

<div class="post-metadata">

**Author:** ![john\_drivenupthewall](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@john\_drivenupthewall](https://forums.sketchup.com/u/john_drivenupthewall)\
**Post date:** [January 30, 2018, 2:15pm UTC](https://forums.sketchup.com/t/ruby-callbacks-not-called-from-javascript/60719/5 "2018-01-30T14:15:18Z")

</div>

@tt_su, why does it change the formatting?

I added some extra css to illustrate what I mean…

 ![html_formatting](https://global.discourse-cdn.com/sketchup/original/3X/e/c/ecf7315481cb63bdd9c10aa59cf944c0a578163a.gif)

the only way to avoid this is to make an entirely new object…

EDIT: this is a mac only bug…

`CommonWebDialog.nib` file, contains a controls box that is being used by `bring_to_front` or a second `show`…

the nib file doesn’t contain a `webView` at all, so that must be generated programatically…

john

---

<div class="post-metadata">

**Author:** ![rajawat](https://avatars.discourse-cdn.com/v4/letter/r/ecd19e/32.png) [@rajawat](https://forums.sketchup.com/u/rajawat)\
**Post date:** [January 30, 2018, 5:13pm UTC](https://forums.sketchup.com/t/ruby-callbacks-not-called-from-javascript/60719/6 "2018-01-30T17:13:42Z")

</div>

Thanks @tt_su.  
Lot to learn.

---

<div class="post-metadata">

**Author:** ![tt\_su](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tt_su/32/17_2.png) [@tt\_su](https://forums.sketchup.com/u/tt_su)\
**Post date:** [January 31, 2018, 1:52pm UTC](https://forums.sketchup.com/t/ruby-callbacks-not-called-from-javascript/60719/7 "2018-01-31T13:52:17Z")

</div>

> [@john\_drivenupthewall](#):
>
> EDIT: this is a mac only bug…

I’ve not seen that before. Can you report this in the Ruby API issue tracker please?

> **[Issues · SketchUp/api-issue-tracker](https://github.com/SketchUp/api-issue-tracker/issues)**
>
> Public issue tracker for the SketchUp and LayOut's APIs - Issues · SketchUp/api-issue-tracker

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/sketchup/original/3X/f/2/f2a121500b085ed60806607a82720b092d800bb3.png) [@system](https://forums.sketchup.com/u/system)\
**Post date:** [May 3, 2018, 5:52am UTC](https://forums.sketchup.com/t/ruby-callbacks-not-called-from-javascript/60719/8 "2018-05-03T05:52:27Z")

</div>

Closed on May 3, 2018, 5:52am UTC

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