sketchucation logo sketchucation
    • Login
    πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    WebDialog - execute_script outside of add_action_callback?

    Scheduled Pinned Locked Moved Developers' Forum
    6 Posts 2 Posters 522 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D Offline
      draftomatic
      last edited by

      Is using execute_script outside of an add_action_callback block allowed?

      My code isn't working unless I do this, but if that's the case, then how do I send information to JavaScript without JavaScript asking for it?

      i.e.
      This doesn't work:

      
      webDiag = UI;;WebDialog.new(...)
      webDiag.set_html(...)
      webDiag.show()
      
      webDiag.execute_script("document.getElementById('myID').value = 'abc'")
      
      

      But this does:

      
      webDiag = UI;;WebDialog.new(...)
      webDiag.set_html(...)
      webDiag.add_action_callback('update') { |web_dialog, params|
        web_dialog.execute_script("document.getElementById('myID').value = 'abc'")
      }
      webDiag.show()
      # Then JavaScript sends "update" back to Ruby...
      
      
      1 Reply Last reply Reply Quote 0
      • thomthomT Offline
        thomthom
        last edited by

        @draftomatic said:

        Is using execute_script outside of an add_action_callback block allowed?

        Yea, should be. Is that your actual test code that doesn't function? Or is it a representation of another script?

        Are you calling webDiag.execute_script immediately after webDiag.show() ?
        In which case that is your problem, the webdialog has not manage to initiate yet.
        What I do is create a load callback, then in the javascript hook into the event that triggers when the DOM has loaded and send a message back to Ruby for the load callback. Then I do the Ruby initiation stuff from there.

        Thomas Thomassen β€” SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund

        1 Reply Last reply Reply Quote 0
        • D Offline
          draftomatic
          last edited by

          @thomthom said:

          @draftomatic said:

          Is using execute_script outside of an add_action_callback block allowed?

          In which case that is your problem, the webdialog has not manage to initiate yet.
          What I do is create a load callback, then in the javascript hook into the event that triggers when the DOM has loaded and send a message back to Ruby for the load callback. Then I do the Ruby initiation stuff from there.

          Cool thanks, I'll try that.

          Follow-up question: Does WebDialog DOM stay loaded when you use WebDialog.close() and then WebDialog.show()? Or do I need to wait again?

          1 Reply Last reply Reply Quote 0
          • thomthomT Offline
            thomthom
            last edited by

            @draftomatic said:

            Follow-up question: Does WebDialog DOM stay loaded when you use WebDialog.close() and then WebDialog.show()? Or do I need to wait again?

            Good question. I don't know.

            However, this can differ from Windows and OSX. As it does with the loading of the HTML.
            From this thread: http://forums.sketchucation.com/viewtopic.php?f=180&t=23445

            @unknownuser said:

            On Windows, the HTML document appear to be created when you call dialog.show. If you attach an event to window.onload which send a command back to ruby to print "Hello World" in the console you'll see "Hello World" printed as you call .show. But on OSX it seems that the HTML document is created as you use .set_file or .set_html - before calling .show.

            Thomas Thomassen β€” SketchUp Monkey & Coding addict
            List of my plugins and link to the CookieWare fund

            1 Reply Last reply Reply Quote 0
            • D Offline
              draftomatic
              last edited by

              @thomthom said:

              From this thread: http://forums.sketchucation.com/viewtopic.php?f=180&t=23445

              Thanks a ton, thomthom - that thread just cured about 16 of my headaches (99 more to go!). The API documentation on WebDialogs is way too minimal...

              1 Reply Last reply Reply Quote 0
              • thomthomT Offline
                thomthom
                last edited by

                Another tip which I might not have mentioned in that article, I find using a javascript framework (like jQuery) takes the pain out of the cross-compatibility JS issues.

                Thomas Thomassen β€” SketchUp Monkey & Coding addict
                List of my plugins and link to the CookieWare fund

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post
                Buy SketchPlus
                Buy SUbD
                Buy WrapR
                Buy eBook
                Buy Modelur
                Buy Vertex Tools
                Buy SketchCuisine
                Buy FormFonts

                Advertisement