sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Javascript MAC innerHTML

    Scheduled Pinned Locked Moved Developers' Forum
    9 Posts 5 Posters 593 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.
    • TIGT Offline
      TIG Moderator
      last edited by

      I've a specialized ruby-based tool that I wrote a year or two ago that uses several webdialogs.
      I've had very few problems with it, both with PC and MAC users, but now I've got one MAC user having problems that I can't figure out...
      In his case the webdialog displays as expected and editable fields fill in and change as he edits... BUT the 'fixed' fields' labels on various parts of the dialog are all 'blank'.
      It's setup as a 'table' with unique 'id=' labels for each of the cells, which all start off as blank by default.
      The ruby code 'populates' the dialog as it opens and it changes the 'innerHTML' of each of these labels [found by their id] to be a text-string, which is translated to suit the user's locale - if there's no matching translation or the locale is already EN-US it should pass as the original EN-US string anyway - and in his case that's what he should see, however, it is "" [blank] in any field where 'innerHTML' is used πŸ˜•
      I conclude that something in his MAC/Safari/Javascript settings [all of which seem to be very 'current'] is adrift and causing the issue?
      I have no MAC to test on and the code-package is somewhat confidential so I can't post too much...
      This is an example - the html says...
      <table id="project_configuration1" style="padding:0px; margin:3px; width:576px; border-style:none;">
      In the ruby code itself it changes the html code's parts' values to suit your entered settings and so on by using javascript calls - and also to update text-fields to suit a locale - when it executes a snippet of javascript to change a cell's "innerHTML", when translating its text-string with deBabelizer [shortcut to db()], e.g.
      js=("document.getElementById(\"project_configuration_label\").setAttribute(\"innerHTML\",\" "+(db("Project Configuration"))+"\");") @dlg.execute_script(js)
      Are there any know issues with MAC/Safari/Javascript failing to change 'innerHTML' strings in webdialogs ??
      There are literally dozens of innerHTML changes made in these dialogs so an easy fix for this lone case would be good πŸ˜•
      Any help/advice appreciated... πŸ˜„

      TIG

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

        because you set an attribute instead of using a property(method) ->
        <div id="project_configuration_label" innerhtml="Project Configuration">

        js=('document.getElementById("project_configuration_label").innerHTML = ' + db("Project Configuration"))
        @dlg.execute_script(js)
        

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

        1 Reply Last reply Reply Quote 0
        • TIGT Offline
          TIG Moderator
          last edited by

          TBD

          Thanks for the advice... but I don't fully understand.
          It works fine on every other PC and MAC installation it's tested on.
          It's just on this one MAC that it fails ??
          It doesn't use 'div' but 'table' and cells in tr/td etc...
          Each id=label has an innerHTML="" even if it's not declared explicitly ?
          Using the javascript to rest the attribute to the required value usually works ??
          So you are saying if the innerHTML is set directly it'll still work AND avoid this rare glitch ???
          That is
          js=("document.getElementById(\"project_configuration_label\").setAttribute(\"innerHTML\", \" " + (db("Project Configuration")) + "\");")
          becomes
          js = 'document.getElementById("project_configuration_label").innerHTML = "' + db('Project Configuration') + '";'
          then
          @dlg.execute_script(js)
          Give or take a (, ), ", ', \ etc or two to get the syntax right πŸ˜•

          BUT... isn't 'innerHTML' an attribute you can set ?
          I can see that setting it direct with innerHTML="" will probably work too... BUT I can't see why my version fails - if you see what I mean ???
          I'll try it and see...

          TIG

          1 Reply Last reply Reply Quote 0
          • fredo6F Offline
            fredo6
            last edited by

            TIG

            I confirm what Dan says. InnerHTML is a property, so you can use directly the dot notation

            Also, when you need to set a property programmatically (i.e. the property name is itself a variable), you have to use the dot notation too

            
            // Set node property
            function j6_set_prop(id, sprop, sval) {
            	node = document.getElementById(id) ;
            	eval("node." + sprop + "= " + sval.toString()) ; 
            }
            
            

            Fredo

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

              I tested your code in my safari 5.0.5 and firefox 4.01 and it fails because it sets an attribute instead of using the property to set the value of the node.

              table:

              <table><tr><td id="mycell">Data</td></tr></table>
              

              code:

              
              cell = document.getElementById("mycell")
              
              cell.setAttribute("innerHTML","XXX")
              > <td id=​"1" innerhtml=​"XXX">​Data​</td>​
              
              cell.innerHTML = "YYY"
              > changes the cell content
              
              

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

              1 Reply Last reply Reply Quote 0
              • TIGT Offline
                TIG Moderator
                last edited by

                Thanks for your help...

                It's odd that for the last year or two it's worked fine on PC AND MAC, so recent changes to Safari/FF must have tightened the javascript/html code interpretation.

                The innerHTML did set fine as an 'attribute' AND a 'property' on all versions... BUT now seems to fail when set as an 'attribute' on the more recent MAC browsers... very weird πŸ˜•

                I'll now recode to use the 'property' method in the dozens of cases πŸ˜’

                Thanks

                TIG

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

                  @tig said:

                  It's odd that for the last year or two it's worked fine on PC AND MAC, so recent changes to Safari/FF must have tightened the javascript/html code interpretation.

                  Recently browsers has picked up development - and I think some are making them stricter in order to push standards. The safest is always to check the specifications and code for that.

                  The last years I've used jQuery exclusively - makes me not have to bother about cross compatibility. Just learn the jQuery API. πŸ˜„ So much happier JS coding.

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

                  1 Reply Last reply Reply Quote 0
                  • TIGT Offline
                    TIG Moderator
                    last edited by

                    Update:-

                    I replaced all of the innerHTML setting as 'properties' rather than as 'attributes'.
                    The dialogs now work [just as before!] on all older PC/MAC installations, AND more importantly on the newer MAC ones too πŸ˜„

                    Thank you all for you help.

                    TIG

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

                      Awhile back, I noticed some quirky behaviour when trying to use innerHTML (on PC with IE7.)

                      I think I switched to using innerText instead and that worked. If you are not actually inserting any HTML tags within the target element, I would say use innerText instead.

                      I have not tested this yet since I upgraded to IE8, (and forget now even what it was I was working on, when I had the problems.)

                      Anyway... FYI, just a note on innerText vs innerHTML.

                      I'm not here much anymore.

                      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