We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6726
    • 7,075 Posts
    A little while ago I posted Have a script.aculo.us effect triggered on page load : how ?? . Boby provided a clean method to do just that, but there is a remaining problem which I have been unable to solve so far...

    When I do use effect on page load, on first load I have a "glitch" : in this example, I have set an effect appear for my image, which is working but when you first load the page, or go to another page and come back, the image first appears without effect, disappears and then appears with the opacity effect. If you reload the page, it works ok with no glitch huh

    It happens with every browser I tested. I googled this, tried forums, but didn’t find anything about this...
    Would anyone happen to have a solution to this... otherwise, it makes using effects on load pretty unusable sad !
      .: COO - Commerce Guys - Community Driven Innovation :.


      MODx est l'outil id
      • 6726
      • 7,075 Posts
      OK guys, I have digged a bit more into this...

      It seems that I have found a solution to this onLoad problem...

      The onload event waits for all binary content to download before firing. No kitty-tickilng until then.

      Dean Edwards has published a fix to the onLoad issue, you can find it here
      Also check this page : DOMContentLoaded for Browsers, Part II

      You can either insert it in the head or as an external file.

      I’ll do some testing to confirm it’s working...


        .: COO - Commerce Guys - Community Driven Innovation :.


        MODx est l'outil id
        • 3763
        • 155 Posts
        Hello David,

        I have found a simple solution to the problem that the image appear first and then dissapears to take the given effect laugh
        Tested with Firefox and Opera, sorry but I have no IE rolleyes

        The tricky thing is to set the initial opacity to 0.0 (with CSS) of the element you will later use the Effect.Opacity function on.
        From your example:
        <div id="image2" style="opacity: 0.0;">
           <img src="images/test_image.jpg" />
        </div>


        You can see here a working example here: http://www.frozenminds.com/lab-files/davidm/

        I will try to find also a solution for the onload event when using the back button.
          ...my Photo Gallery on Flickr...
          • 6726
          • 7,075 Posts
          lol, I am SO dumb !

          so simple that I just did not even think to try this !
          It’s the same principle used with effect appear, you apply an inline display:none; to the element rolleyes

          Again you’re saving me !
          If you drop by Paris, I owe you a bunch of drinks and a good meal grin

          The Dean Edwards stuff did not work, finally...
            .: COO - Commerce Guys - Community Driven Innovation :.


            MODx est l&#39;outil id
            • 25663 MODX Staff
            • 12,272 Posts
            Keep in mind that this fix won’t be degradable for browsers that don’t support Ajax effects or if something goes awry (image will be permanently transparent).
              Ryan Thrash, MODX Co-Founder
              Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
              • 6726
              • 7,075 Posts
              Thanks for pointing that out, Ryan. Toying with those effects, it’s pretty easy to forget that I admit...

              Same goes for effect appear, I used that before for the Amadom website and had put up a <noscript> alternative. I wouldn’t want to break accessibility smiley

              I’ll look into a better way that a noscript tag for alternate content... if you know any, I am interrested !
                .: COO - Commerce Guys - Community Driven Innovation :.


                MODx est l&#39;outil id
                • 3763
                • 155 Posts
                I have found a new solution smiley actually two of them.

                1) After Ryan’s suggestion, I thought it would be OK to change the class name also with Javascript. In case a visitor has it disabled, the image will show up normally. If Javascript is enabled, the initial image opacity is set to zero.
                $('image3').className = 'opacity-workaround';
                new Effect.Opacity('image3', {duration:0.9, from:0.0, to:1.0});

                Where the CSS styling is like before:
                .opacity-workaround
                {
                   opacity: 0.0;
                }


                2) To have the onload effect working even when using the "back" button in the browser, you can disable the browser cache with PHP. Works also with HTML meta tags.
                I have used in this example PHP because it’s easy and you could write a simple plugin for your MODx powered website.
                Just add following code on top of your file and you should be OK, no need for other Javascript workarounds:
                <?php
                   @ header ('Expires: Mon, 14 Oct 2002 05:00:00 GMT');               //Date in the past
                   @ header ('Last-Modified: ' . gmdate ("D, d M Y H:i:s") . ' GMT'); //Always modified
                   @ header ('Cache-Control: no-store, no-cache, must-revalidate');   //HTTP 1.1
                   @ header ('Cache-Control: post-check=0, pre-check=0', false);
                   @ header ('Pragma: no-cache');                                     //HTTP 1.0
                ?>


                With HTML meta tags is goes like this:
                <meta http-equiv="Pragma" content="no-cache" />
                ...and so on...


                A full working example you can find here: http://www.frozenminds.com/lab-files/davidm/ (updated)
                Here is also full source smiley
                What I have also added is an IF clause before executing something on an item, so it will run only if the item is on the page. This was a useful method for some IE bugs/errors on my first AJAX script (for another software).

                <?php
                   @ header ('Expires: Mon, 14 Oct 2002 05:00:00 GMT');               // Date in the past
                   @ header ('Last-Modified: ' . gmdate ("D, d M Y H:i:s") . ' GMT'); // Always modified
                   @ header ('Cache-Control: no-store, no-cache, must-revalidate');   // HTTP 1.1
                   @ header ('Cache-Control: post-check=0, pre-check=0', false);
                   @ header ('Pragma: no-cache');                                     // HTTP 1.0
                ?>
                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                <html xmlns="http://www.w3.org/1999/xhtml">
                <head>
                   <title>A simple workaround for Effect.Opacity</title>
                   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                   <script type="text/javascript" src="javascripts/prototype/prototype.js"></script>
                   <script type="text/javascript" src="javascripts/scriptaculous/scriptaculous.js"></script>
                   <style type="text/css">
                   /* <![CDATA[ */
                
                   /* We set the initial opacity of the element
                      that we'll later transfor with Effect.Opacity to 0.0 (zero) */
                   .opacity-workaround
                   {
                      opacity: 0.0;
                   }
                
                   /* ]]> */
                   </style>
                   <script type="text/javascript">
                   /* <![CDATA[ */
                
                   /* Observe when page loads and run the "init" function */
                   Event.observe(window, 'load', init, false);
                
                   /* When page is loaded, this function is called */
                   function init()
                   {
                      //Incorrect example
                      if ($('image1'))
                      {
                         new Effect.Opacity('image1', {duration:0.9, from:0.0, to:1.0}); //Run effect
                      }
                
                      //Correct examples
                      if ($('image2'))
                      {
                         $('image2').className = 'opacity-workaround'; //Change class
                         new Effect.Opacity('image2', {duration:0.9, from:0.0, to:1.0}); //Run effect
                      }
                      if ($('image3'))
                      {
                         $('image3').className = 'opacity-workaround'; //Change class
                         new Effect.Opacity('image3', {duration:0.9, from:0.0, to:1.0}); //Run effect
                      }
                   }
                
                   /* ]]> */
                   </script>
                </head>
                <body>
                   <h4>Image will show up first, then dissapear and fade in (NOT GOOD)</h4>
                   <div id="image1">
                      <img src="images/leaf.jpg" alt="Leaf on rocks" />
                   </div>
                
                   <h4>Image won't show up first but fade in nicely (GOOD)</h4>
                   <div id="image2">
                      <img src="images/leaf.jpg" alt="Leaf on rocks" />
                   </div>
                
                   <div id="image3">
                      <img src="images/leaf.jpg" alt="Leaf on rocks" />
                   </div>
                </body>
                </html>


                Maybe there are other ways to solve this, but I haven’t found any other tongue



                Quote from: davidm at Sep 11, 2006, 10:32 AM
                Again you’re saving me !
                If you drop by Paris, I owe you a bunch of drinks and a good meal grin
                Relax man laugh
                I owe you more than a simple workaround for script.aculo.us because you gaved me a great CMS laugh


                Hope this helps!
                Boby
                  ...my Photo Gallery on Flickr...
                  • 6726
                  • 7,075 Posts
                  One word : this is great !

                  Quote from: Boby at Sep 11, 2006, 12:11 PM
                  2) To have the onload effect working even when using the "back" button in the browser, you can disable the browser cache with PHP. Works also with HTML meta tags.

                  Very nice !

                  I didn’t test it in other browsers, but in FF and Safari, hitting the back button without any additionnal code works OK. Would that be an IE fix only ?

                  Quote from: Boby
                  Relax man laugh I owe you more than a simple workaround for script.aculo.us because you gaved me a great CMS laugh

                  Um, in fact that would be Ryan, Jason and Raymond tongue
                  I am just the marketing guy wink

                  Don’t worry, I am relaxed alright : I like good food, good wine (and beer too), I know my way around Paris and most definitely will enjoy introducing the great people at MODx to Paris, and have a chat around a good table wouldn’t hurt grin And Paris sure would make your Nikon D50 happy (funny, I have the same DSLR smiley What lens did you get ? I took a simple 18-200 from sigma, which is nice)

                  Quote from: Boby
                  Hope this helps!

                  It sure does, on top of that you just provided an elegant way to do it which is always better !
                  I am bookmarking your posts, we could end up with great material to build javascript libraieries tutorials smiley

                  And have some enhanced template too laugh

                    .: COO - Commerce Guys - Community Driven Innovation :.


                    MODx est l&#39;outil id
                    • 3763
                    • 155 Posts
                    Quote from: davidm at Sep 11, 2006, 01:15 PM
                    Would that be an IE fix only ?
                    Actually I don’t know smiley
                    I added some JS code with the onload event, but it was loaded on all pages. When the items where not found, IE triggered an error. So I used the IF clause tongue

                    Quote from: davidm at Sep 11, 2006, 01:15 PM
                    Don’t worry, I am relaxed alright : I like good food, good wine (and beer too), I know my way around Paris and most definitely will enjoy introducing the great people at MODx to Paris, and have a chat around a good table wouldn’t hurt grin And Paris sure would make your Nikon D50 happy (funny, I have the same DSLR smiley What lens did you get ? I took a simple 18-200 from sigma, which is nice)
                    Ooohh, another photo freak laugh
                    I have the standard 18-55mm kit lens. Today I ordered a used 75-300mm, it’s old (1999) and heavy (850g) buy very sharp. I liked this one more over the newer plastic tele lenses with a resonable price.
                    That all is just another reason to plan my next holidays for Paris laugh Hope you know some nice girls HaHaHa

                    Quote from: davidm at Sep 11, 2006, 01:15 PM
                    It sure does, on top of that you just provided an elegant way to do it which is always better !
                    I am bookmarking your posts, we could end up with great material to build javascript libraieries tutorials smiley
                    Oh great smiley
                    But we should definatly learn more JS for libraries. I’m also not very experienced with it tongue

                    Have a nice day!
                    Boby
                      ...my Photo Gallery on Flickr...
                      • 6726
                      • 7,075 Posts
                      Me again. There is a strange thing for me : while your demo page works fine in Safari, the web page I tried it on does not (I used the same code, and did not find what’s wrong with it sad )

                      It works fine with IE 6 / WinXP, FF Win and Mac, Opera 9 Win and Mac but not Safari huh
                      Any idea ?

                      Edit : Note that the quick fix worked in Safari flawlessly
                        .: COO - Commerce Guys - Community Driven Innovation :.


                        MODx est l&#39;outil id