1 2 Previous Next 22 Replies Latest reply on Aug 28, 2014 8:41 AM by shae1725

    Javascript var and FM decimal number fields

    shae1725

      Hi hope some one can help

       

      I am trying to create a var in javascript using a filemaker field as example below. I cant get the field contents into the variable.

       

      var sizescaled = "" & table::field & "";

       

      table::field is a number field

       

      the values are always with decimal points and usually less than "1" ie: .3459823478654323

       

      I know my script works because if I manually paste the field value to the var ie: ( var sizescaled = .3459823478654323 ) all works just fine.

       

      I have other vars in my javascript which work just fine (these are number fields BUT with whole number values)

       

      I really would appreciate any help

        • 1. Re: Javascript var and FM decimal number fields
          beverly

          In your "pasted" version you don't have quotes, but in your field example, you do have quotes around the number.

           

          I'm assuming this is a calculation:

           

               calcfield = "var sizescaled = " & table:field

           

          That's all you should need if the var you are setting is a number (any precision). You don't say what the rest of the calculation is (if any), so this may or may not be correct.

           

           

          HTH,

          Beverly

          • 2. Re: Javascript var and FM decimal number fields
            shae1725

            Hi beverly

             

            thanks for your reply

             

            I am new to javascript and I assumed that any filemaker field had to be enclosed with \"" &  fm::field & "\"

             

            There is no other calculation involved, its simply to fetch the fm fields value which I pass to other parts of my script.

             

            I have added a snip of the script which might show what I am trying to do.

             

            the highlighted areas are fields that are working properly and doing exactly what I want.

            the red areas show the var and where I am using the it

             

            many thanks for your helpjava.PNG

            • 3. Re: Javascript var and FM decimal number fields
              TomHays

              In your snip image it appears that the part you are showing is the middle section of a quoted piece of text.  Thus all of the quotes you need in your HTML are escaped with a backslash, e.g. <script type="text/javascript"> becomes <script type\"text/javascript\">.

               

              When you are inserting the value of a FileMaker field, you are ending the quote environment, concatenating the field value, and then concatenating a new quote environment.

               

              In your snip I think the first circled item should be

               

              var drawscale = " & Cad::drawScale & ";

               

              The first quote in the line above is ending a quote in a FileMaker expression that spanned many previous lines (with the opening quote not visible in your snip).  The second quote starts a new filemaker quote string that spans multiple lines that follow it.

               

              If you needed to quote the assignment in JavaScript then you would need to add escaped quotes around the value.

               

              var drawscale = \"" & Cad::drawScale & "\";

               

               

              -Tom

              • 4. Re: Javascript var and FM decimal number fields
                beverly

                No, if the calculation is calling the field ALL OTHER bits need to be quoted, but the field does not.

                 

                by your screen shot it look like you are putting the javascript into a field and trying to get the value from other fields into it. That won't work unless the field is "calculated text"

                 

                "

                var layer1;

                var layer2;

                ...

                var drawscale = "   <<-- note the END of the text at this point so a field's value can be inserted

                & Cad::drawScale &

                ";

                 

                layer1 =

                ...

                " <<--- end of text as double quote

                 

                  • note that there is \" around the text/javascript. This could be avoided if you use single quote for embedded HTML/JS/CSS:

                 

                "

                var layer1;

                var layer3;

                ...

                " <<--- end of text as double-quote

                 

                Please describe in more detail how this is being used.

                 

                Beverly

                • 5. Re: Javascript var and FM decimal number fields
                  shae1725

                  Hi

                   

                  now I am totally confused

                   

                  why when I manually place a value in the code instead of a fm field all works ok?

                  reason for asking is that if you look at rectangles being drawn , the width and height are referenced fm fields, should they just be & table::field & and if so why does it work?

                   

                  I am using a webviewer to display drawn objects on  canvases

                  the drawscale field is used to scale the drawings as their dimensions are manipulated The calculations for this field and all the others are taking place in filemaker although as I have noticed as I get used to javascript they could all be done with it. I had prviously designed my solution using monkeybreads graphicmagick functions so using the same fields but after seeing what can be done with html5 canvas I decided to give it a go

                   

                   

                  200000.PNG

                  • 6. Re: Javascript var and FM decimal number fields
                    beverly

                    The "reference" to another field within a text field will not be "evaluated". If the field is a calculation type field, it can be evaluated.

                     

                    You are trying to create "code" that is just text, but supplied with values from other field(s).

                     

                    Putting the code inside a "web viewer" is like putting it inside a calculation dialog, and it's evaluated. But the text has quotes and the references to the other fields do not.

                     

                    ""

                     

                    Beverly

                    • 7. Re: Javascript var and FM decimal number fields
                      erolst

                      Some notes and suggestions that (hopefully) will do away with your confusion and help you write your code more easily:

                       

                      1. You're essentially building a string; that this happens to be valid code in another environment is beside the point; the usual rules of string-building in FileMaker apply.

                       

                      2. If you quote a field reference, or place it inside a string, FileMaker cannot evaluate it; it's just a part of a 'dumb' string.

                       

                      3. Numbers that are passed as numeric arguments should not be quoted; AFAIK, this holds true for all (modern) programming environments.

                       

                      4. Instead of escaping all those (necessary) double-quotes, either use the Quote() function, or even simpler, use single-quotes, which JavaScript and HTML will happily accept.

                       

                      5. Use the Let() function; this will optimize the code for both you (better readable, depending on your variable names) and FileMaker (fewer field accesses). Win-win, as they say …

                       

                      6. For trouble-shooting, calculate your code in a field and reference that field in your Web Viewer. Place the field on the layout to see the calculated HTML; this should help to spot obvious errors.

                       

                      See attached file for a number of ways to combine string literals and field (or variable) references.

                      • 8. Re: Javascript var and FM decimal number fields
                        shae1725

                        Hi

                         

                        thanks for all your help

                         

                        just looked at your demo! think I should have done a bit of research before I started,

                        I can see already even with little experience that I can achieve results.

                        • 10. Re: Javascript var and FM decimal number fields
                          shae1725

                          Hi

                           

                          Really hope you can help as I am really stuckscale2.PNGscaling1.PNG

                           

                          I rebuilt my calculation as you recommended and I have to say I learnt alot and have a better understanding on how to progress.

                           

                          Unfortunately the problem with the decimal and the scale function is still lurking!

                           

                          I set the variable for scale in the let function    drawscale = table::field       it doesnt work

                          If I set the variable in the let with the value of the field ie    drawscale = "0.678989878789"   this works!

                           

                          In the field you recomended me to add to my layout I noticed that when drawscale =table::field is used

                          that a coma instead of a fullstop is between the 2 values in the scale function.

                           

                          I have added a snip of the field for a visual. the second snip is the the var set as a fixed value in the let and it works ok but I cant help but notice a coma in the first value.

                           

                          Thanks

                          • 11. Re: Javascript var and FM decimal number fields
                            erolst

                            Why don't you post your file?

                             

                            From those snippets, which show the end result of a calculation, not the calcuation itself, it's hard to tell what causes the difference; and I have no idea of the values in your fields or the calculations you perform to get these compound values.

                            • 12. Re: Javascript var and FM decimal number fields
                              beverly

                              I have not seen the demos posted here. But another method is to use a text field and "placeholders":

                               

                                   *field containing:

                               

                              var x = 'abc';

                              var y = 123;

                              var z = <<field1>>;

                              • 13. Re: Javascript var and FM decimal number fields
                                shae1725

                                Hi Beverly

                                 

                                Sorry but I dont fully understand

                                 

                                I've only started to learn html and javascript so apologies for sounding a bit thick.

                                 

                                does it go in the javascript or the let function and do I have to add to each context.

                                • 14. Re: Javascript var and FM decimal number fields
                                  shae1725

                                  going to build a small version for testing purposes and will post it up here if I havent had any success solving the problem in between times.

                                   

                                  Thanks again

                                  1 2 Previous Next