Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

wait until HTML.js loaded

Help
Eric
2009-02-18
2013-04-29
  • Eric
    Eric
    2009-02-18

    hi,

    i have been using the easy/load.js approach. i would like to call a function after HTML.js has loaded. i added this to the beginning of easy/load.js:

    jsMath = {
      Setup: {
        UserEvent: {
          onload: function() {
        theFunctionIWantToCall();
          }
        }
      }
    }

    this works but i would like it to wait until after HTML.js is loaded. i am not sure how to make that happen.

    thanks for any help
    best
    eric

     
    • Eric
      Eric
      2009-02-19

      hi,

      i think i have figured out a workable approach. not sure it is optimal. i started with an ugly hack, simply adding a call at the end HTML.js to my function. but what i really needed was for the \class and \CssID commands in my page to be executed prior to my function was being called (otherwise the page wasn't really set up first).

      so now i have this and it seems to work.

      jsMath.Setup.Script("plugins/tex2math.js");
          jsMath.Setup.Script("extensions/HTML.js");
          jsMath.Synchronize("jsMath.ConvertLaTeX()");
          jsMath.Synchronize("jsMath.Process()");
          jsMath.Synchronize("theFunctionIWant()");

      best,
      eric

      ps for what it's worth, i think jsMath rules---what a great tool.

       
      • I was going to recommend something like what you have done, but I'm glad you figured it out on your own.

        Note, however, that jsMath.Setup.Script(), jsMath.ConvertLaTeX(), and jsMath.Process() already include synchronization, so do not require jsMath.Synchrinize().  Only your custom function does.  Also, it is more efficient to call

        jsMath.Synchronize(theFunctionIWant);

        passing the function itself rather than a string that will have to be compiled at runtime.
        So I'd say a slightly better solution would be:

        jsMath.Setup.Script("plugins/tex2math.js");
        jsMath.Setup.Script("extensions/HTML.js");
        jsMath.ConvertLaTeX();
        jsMath.Process();
        jsMath.Synchronize(theFunctionIWant);

        Hope that works for you.

        Davide

         
        • Eric
          Eric
          2009-02-19

          thank you davide.

          i tried your suggestions. as you say, removing ConvertLaTeX() and Process() from the Synchronize queue works fine.

          however, interestingly, i get two different behaviors depending on whether i pass my function as a string vs. a function. first, i try this:

              jsMath.Setup.Script("plugins/tex2math.js");
              jsMath.Setup.Script("extensions/HTML.js");
              jsMath.ConvertLaTeX();
              jsMath.Process();
              jsMath.Synchronize(myFunction());

          then i can see the \class command is executed *after* myFunction is called. i can see this because i added a i line to HTML.js for logging that logs to the firebug console when the \class function is called.

          here are the firebug's console log messages:

            calling myFunction
            calling class with arg: hinttarget

          but if i quote the synchronize call, e.g., Synchronize("myFunction()"), then i get this output instead:

            calling class with arg:  hinttarget
            calling myFunction

          the reason this matters to me is that "myFunction" is looking for page elements labeled by the \class call. if myFunction is called first, those elements aren't yet initialized.

          you mentioned that passing myFunction as a string requires a compilation step. could (?) this be just the delay needed to explain why the \class command is being executed prior to myFunction, and that by removing the compilation step, myFunction bumps forward in the sequence?

          or am i doing something boneheaded? myFunction uses a jQuery call, if that is at all useful.

          thanks again for your help

          eric

           
          • I'm afraid you are doing something boneheaded.  :-)  But it is an easy mistake to make, and one we have all done at one time or another.  Notice that I suggested you do

            jsMath.Synchronize(myFunction);

            not

            jsMath.Synchronize(myFunction());

            as you have listed in your example above.  Do you see the difference?  The first passes the function object to jsMath.Synchronize (and it eventually calls the function at the right time).  The second passes the RESULT of calling myFunction, which means myFunction is called BEFORE jsMath.Synchronize is called, and so the myFunction call is NOT being synchronized with jsMath, as desired.

            If you take away the parentheses, I think it will work for you.  If you aren't used to thinking of functions as data in their own right, this kind of error is easy to make.

            Davide

             
            • Eric
              Eric
              2009-02-19

              thanks davide! not only for helping me out but for tolerating my cranial density. that works great & your explanation makes sense. i have made this same mistake before. you'd think it would have sunk in. but my head is 98% bone to begin with so maybe there is no hope ;-)

              best
              eric