sketchucation logo sketchucation
    • Login
    🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Web Dialog not firing a JavaScript function...

    Scheduled Pinned Locked Moved Developers' Forum
    14 Posts 5 Posters 2.6k 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.
    • A Offline
      ArchToBe
      last edited by

      Greetings to all,

      I'm just starting to experiment with web dialogs, and it's not going very well. At this point, I'm simply trying to run a very simple JavaScript function from Ruby. Below are two very simple scripts - one Ruby and one html/JavaScript. All I'm trying to do is get Ruby to launch a JavaScript function which fills a textbox. However, although the browser successfully loads the html file, it appears the JavaScript function is not being fired. I get no error messages, however.

      Can you see what I'm doing wrong here?

      THANKS!


      -------- My Ruby Script... --------------------

      
      # An experiment to fill an html dropdown list from a ruby script.
      
      wd=UI;;WebDialog.new
      path=Sketchup.find_support_file "JavaScriptTest1.htm", "plugins"
      wd.set_file path
      wd.show
      
      wd.execute_script("fillTextBox();") #does NOT work!
      
      

      -------- My html/JavaScript file ---------------

      
      <html>
      	<head>
      		<script type="text/javascript">
      
      			function btnButton_onclick()
      				{
      				    document.form1.txtBox.value = "You clicked !!!";
      				}
      
      			function fillTextBox()
      				{
      				    alert("Entering fillTextBox()");
          				document.form1.txtBox.value = "Hello from Ruby !!!";
                      }
      
      		</script>
      	</head>
      	<body>
      	    <form name="form1">
          		<br />
      	    	Output; <input type="text" name="txtBox" size="15">
      		    <br>
      		    <br>
      		    <input type="button" name="btnButton" value="Go!" onclick="btnButton_onclick()">
      		</form>
      	</body>
      </html>
      
      
      1 Reply Last reply Reply Quote 0
      • A Offline
        ArchToBe
        last edited by

        Sorry, I forgot to mention what I'm using...

        Sketchup 7.1.6860
        IE 7

        1 Reply Last reply Reply Quote 0
        • J Offline
          Jim
          last edited by

          Looks good - but maybe try to defer the execute_script by a short time just to see if it is a timing problem - calling the function before dialog has a chance to completely load.

          
          UI.start_timer(1, false) { wd.execute_script("fillTextBox();") }
          
          

          Hi

          1 Reply Last reply Reply Quote 0
          • M Offline
            Mr.K.1
            last edited by

            It's true, during the time webdialog loads all commands to it will be ignored, but timers are not the best system to avoid it because you never know how long it will take on someones machine.

            So a much cleaner way that I only recently found out about:

            wd=UI;;WebDialog.new
            
            wd.show{
             wd.execute_script("fillTextBox();")
            }
            
            

            All commands inside the "show" block it will be run right after the webdialog shows, so there will be no loss of time.

            1 Reply Last reply Reply Quote 0
            • Dan RathbunD Offline
              Dan Rathbun
              last edited by

              The block for the show (and show_modal,) methods seem to only work on PC with MSIE. Last I knew, Safari on Mac, ignored the block for these methods.

              An alternative, is to do it on the JS side with an onload event, attached to the document or body. There are several topics here that discussed this, and I recall ThomThom and TIG testing this issue on both platforms.

              EDIT(Add links):

              • Webdialog Question on .show
              • PC v MAC webdialog populate
              • Assigning Event Handlers in JS-Written UI, Painlessly
              • When Ruby Can't Execute_Script

              I'm not here much anymore.

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

                I prefer to not use the onload event, but instead use the event that triggers when the HTML DOM is ready. onloads wait until all the images and resources as loaded, but the DOM load triggers earlier. When using local files there might not be much of a noticeable difference, but in some cases you might get a visual glitch or lag before onload triggers.

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

                1 Reply Last reply Reply Quote 0
                • A Offline
                  ArchToBe
                  last edited by

                  Yes! You guys were absolutely right - it was the need for a delay which was causing the problem. As always, you guys are THE BEST!!!

                  Thomas, I notice you're located in Norway. We here in the US (and elsewhere I'm sure) are saddened by what has happened in Oslo. Please know that our thoughts are with you. Take care.

                  1 Reply Last reply Reply Quote 0
                  • J Offline
                    Jim
                    last edited by

                    @thomthom said:

                    I prefer to not use the onload event, but instead use the event that triggers when the HTML DOM is ready. onloads wait until all the images and resources as loaded, but the DOM load triggers earlier. When using local files there might not be much of a noticeable difference, but in some cases you might get a visual glitch or lag before onload triggers.

                    So Thomas, might this be an opportunity to get started on the Ruby API Bugtracker?

                    Hi

                    1 Reply Last reply Reply Quote 0
                    • Dan RathbunD Offline
                      Dan Rathbun
                      last edited by

                      @thomthom said:

                      I prefer to not use the onload event, but instead use the event that triggers when the HTML DOM is ready.

                      What was that event again? onReadyStateChange or onWebpageComplete ??

                      I'm not here much anymore.

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

                        @dan rathbun said:

                        What was that event again? onReadyStateChange or onWebpageComplete ??

                        Link Preview Image
                        dean.edwards.name

                        favicon

                        (dean.edwards.name)

                        @jim said:

                        So Thomas, might this be an opportunity to get started on the Ruby API Bugtracker?

                        Oh -yea. Nearly forgotten about that. Please do fill in issues and get it started. I'll remember to being using it myself when I'm back from vacation.

                        @archtobe said:

                        Thomas, I notice you're located in Norway. We here in the US (and elsewhere I'm sure) are saddened by what has happened in Oslo. Please know that our thoughts are with you. Take care.

                        Appreciate it. I'm currently on vacation in Gøteborg and learned about it a little bit later that day when people started texting me asking if I was ok. Myself and my friends are all ok - as far as I'm aware. Still shocked about the coldbloodedness of this madman.

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

                        1 Reply Last reply Reply Quote 0
                        • Dan RathbunD Offline
                          Dan Rathbun
                          last edited by

                          @thomthom said:

                          @dan rathbun said:

                          What was that event again? onReadyStateChange or onWebpageComplete ??

                          found it:
                          [onReadyStateChange event (MSIE 7+)](http://msdn.microsoft.com/en-us/library/ms536957(v)

                          ... which uses the [readyState property](http://msdn.microsoft.com/en-us/library/ms534359(v)
                          You can test for values "complete" (everything is loaded,) or "interactive" (if the document is ready for user interaction.)

                          I'm not here much anymore.

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

                            It's the DOMContentLoaded event I linked to...

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

                            1 Reply Last reply Reply Quote 0
                            • Dan RathbunD Offline
                              Dan Rathbun
                              last edited by

                              @thomthom said:

                              It's the DOMContentLoaded event I linked to...

                              which doesn't work for MSIE (IE 9+ does have some domContent... Timing properties but the page must set to IE9 Standards mode.)

                              Did you see that author's addendum page? (It uses onReadyStateChange event for MSIE to avoid the separate script file.)
                              http://dean.edwards.name/weblog/2006/06/again/

                              I'm not here much anymore.

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

                                Yes, one has to use a combo. If you look at the jQuery source you see the various methods they use to make it cross platform compatible. onReadyStateChange is used as a fallback for older IE versions. Mozilla, Opera, Webkit and newer IE use DOMContentLoaded.
                                http://ie.microsoft.com/testdrive/HTML5/DOMContentLoaded/Default.html

                                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