sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    πŸ«› Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download

    Assigning Event Handlers in JS-written UI

    Scheduled Pinned Locked Moved Developers' Forum
    11 Posts 5 Posters 452 Views 5 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.
    • M Offline
      MartinRinehart
      last edited by

      Could it be possible that I am the first one trying to make a UI that depends on the content of the model? Anyone?

      Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

      1 Reply Last reply Reply Quote 0
      • Chris FullmerC Offline
        Chris Fullmer
        last edited by

        Its something that I am preparing to develop in my next script. But so far I have not touched it.

        Chris

        Lately you've been tan, suspicious for the winter.
        All my Plugins I've written

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

          @martinrinehart said:

          Could it be possible that I am the first one trying to make a UI that depends on the content of the model? Anyone?

          A while back I was working on an Area reporter. Taking all the faces in the model and building a list of the areas for each material grouyped by component/groups-

          From working with website before I never use document.write, but instead add elements using the DOM functions as that always works.

          
          var btn = document.createElement('button');
          btn.appendChild( document.createTextNode('Button Text') );
          btn.onclick = clicker2;
          
          

          It's more code to write though - so I often write wrapper functions. Or rather - lately I've started using jQuery, even for SU webdialogs.

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

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

            (ps. my sample code didn't display the element being added to the document.)

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

            1 Reply Last reply Reply Quote 0
            • M Offline
              MartinRinehart
              last edited by

              @thomthom said:

              From working with website before I never use document.write, but instead add elements using the DOM functions as that always works.

              Thank you.

              "... always works." I pray you are right, but I'm skeptical.

              Martin

              Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

              1 Reply Last reply Reply Quote 0
              • M Offline
                MartinRinehart
                last edited by

                @martinrinehart said:

                but I'm skeptical

                This works:

                <!-- a.html -->
                <html> <body onload=init()>
                	
                <script src='a.js'> </script>
                
                </body> </html>
                

                Where the DOM is created this way:

                // a.js
                
                var clicker  = function() { alert('That tickles!'); }
                function clicker2() { alert('That tickles, too!'); }
                
                function init() {
                	var d = document;
                
                	var b1 = d.createElement( 'button' );
                	b1.innerHTML = 'Click me!';
                	b1.onClick = clicker;					
                	d.body.appendChild( b1 );
                
                	var b2 = d.createElement( 'button' );
                	b2.innerHTML = 'Me too!';
                	b2.onClick = clicker2;					
                	d.body.appendChild( b2 );
                
                alert( b1.onClick + '\n\n' + b2.onClick );
                }
                
                

                The only problem is, nothing happens when you click the buttons. Presumably a browser bug (v8). Workaround?

                Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

                1 Reply Last reply Reply Quote 0
                • M Offline
                  MartinRinehart
                  last edited by

                  @martinrinehart said:

                  The only problem is, nothing happens when you click the buttons. Presumably a browser bug (v8).

                  Somebody help me out here. I hate being stupid in public.

                  This code doesn't work:

                  
                  	var b2 = d.createElement( 'input' );
                  	b2.type = 'checkbox';
                  	b2.onclick = clicker2;
                  	d.body.appendChild( b2 );
                  
                  

                  If you reverse the last two lines, appending to body before assigning to onclick, the onclick handler starts to work. (This after many tries in Chrome, Firefox, Opera and that other browser. My Google search engine is starting to say, "Oh, please! Not again!")

                  I now have a JS-generated checkbox that can call an onclick handler and I am going back to work on my Ruby. Anyone have an explanation for the above "gotcha"?

                  Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

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

                    Again - I'm not sure. As I mentioned before I now usually use the jQuery framework. And that adds event listeners. Event listeners let you add multiple functions to the same event.

                    JavaScript tutorial - DOM events

                    favicon

                    (www.howtocreate.co.uk)

                    Adding event listeners might need some different treatment with your favourite browser πŸ˜‰ . So it can easily end up with extra code.

                    Den Edward got, as usual, a good cross browser wrapper that you can use without using a framework to simply code. http://dean.edwards.name/weblog/2005/10/add-event/

                    I'm not sure why adding the element to the document before adding events works - and not the other way around. Maybe the added element is a copy and not a reference - and copies doesn't transfer events. (guessing here - need to fresh up on this)

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

                    1 Reply Last reply Reply Quote 0
                    • tbdT Offline
                      tbd
                      last edited by

                      some dynamic code that works on my Windows machine and creates another checkbox when you click on existing ones:

                      
                      w = UI;;WebDialog.new
                      w.set_html("<body>hello</body><script>add();function add(){var d = document;var b2 = d.createElement( 'input' );b2.type = 'checkbox';b2.onclick = function(){add();};d.body.appendChild(b2)}</script>")
                      w.show
                      
                      

                      SketchUp Ruby Consultant | Podium 1.x developer
                      http://plugins.ro

                      1 Reply Last reply Reply Quote 0
                      • M Offline
                        MSP_Greg
                        last edited by

                        Martin,

                        1. I write all my code for Windows & IE
                        2. I noticed some of your code had attributes that were not quoted. W3C says...
                        3. Sometimes I assemble/modify the html as a string, then use set_html. All of my html is loaded as a string
                        4. I've got 3 or 4 'dynamic' web dialogs in an extension at
                          http://www.ChampionEnt.net/tech/ur/
                          if any of it might interest you, let me know and I can post or send the code.
                        5. Due to the 'modal window' issue, some of the code only works on a PC (settings window)
                        6. Haven't really tested it lately on an Apple...
                        7. Wrote some C# code to change an html file into a Ruby friendly string

                        HTH,

                        Greg

                        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