Javascript Loading Mask

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eragon
    Contributor
    • Mar 2007
    • 431

    Javascript Loading Mask

    JavaScript Loading Mask

    Requirements: Little knowledge in JavaScript and some HTML. JavaScript enabled browser.
    Applications: Useful for all those pages that load slow, and in sections, so instead of watching the page load in sections.


    Put this in your <head>:
    [code=javascript]<script type="text/javascript">
    function overlay() {
    elem = document.getEle mentById("overl ay");
    elem.style.visi bility="hidden" ;
    elem = document.getEle mentById("bodyd iv");
    elem.style.visi bility="visible ";
    }
    </script>[/code]



    Make your <body> tag look like this:
    [code=html]<body onLoad="overlay ()">[/code]



    Inside your <body>, at the top of everything, put this:
    [code=html]<div id="overlay" style="width:10 0%; height:100%; position: absolute; background-color:#000000;" >
    <table><tr><t d valign="center" height="100%" width="100%">
    <div align="center" style="border: 1px solid black; background-color:#ffffff; width:50%;">
    <h3>Loading.. . Please Wait.</h3>
    </div>
    </td></tr></table>
    </div>
    <div id="bodydiv" style="visibili ty:hidden;">
    ... (your current page body) ...<br />
    ... (Which should be long..) ...<br />
    ... (because it has to load) ...<br />
    ... (so yeah.. this is body) ...
    </div>[/code]



    So all together you get:
    [code=html]<html>
    <head>
    <title>Loadin g Screen</title>
    <script type="text/javascript">
    function overlay() {
    elem = document.getEle mentById("overl ay");
    elem.style.visi bility="hidden" ;
    elem = document.getEle mentById("bodyd iv");
    elem.style.visi bility="visible ";
    }
    </script>
    </head>
    <body onLoad="overlay ()">

    <div id="overlay" style="width:10 0%; height:100%; position: absolute; background-color:#000000;" >
    <table><tr><t d valign="center" height="100%" width="100%">
    <div align="center" style="border: 1px solid black; background-color:#ffffff; width:50%;">
    <h3>Loading.. . Please Wait.</h3>
    </div>
    </td></tr></table>
    </div>
    <div id="bodydiv" style="visibili ty:hidden;">
    ... (your current page body) ...<br />
    ... (Which should be long..) ...<br />
    ... (because it has to load) ...<br />
    ... (so yeah.. this is body) ...
    </div>

    </body>
    </html>[/code]



    And you have a pleasant loading message. You may edit the content (div styles, etc.) but the JavaScript must remain as is, unless you know what you are doing. This is really a simple script, but it is there for those that want it.

    Sincerely,
    Eragon
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Are you sure you've got the background color correct - o or 0?

    Remember, Java != JavaScript.

    Otherwise, I'm sure people will find this useful.

    Comment

    • eragon
      Contributor
      • Mar 2007
      • 431

      #3
      Background color fix: #000000

      and thats all i presume. Please check for compatibility.. .

      thank you :)

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by eragon
        Background color fix: #000000

        and thats all i presume. Please check for compatibility.. .
        Fixed. I'll check it later.

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          There was a mistake in the full version of the code in the body onload (line 13). You forgot the parentheses/brackets. I've added them for you.

          This seems to work well, but you should consider changing the colors and font - black and white is a bit boring!

          Comment

          • eragon
            Contributor
            • Mar 2007
            • 431

            #6
            Originally posted by acoder
            There was a mistake in the full version of the code in the body onload (line 13). You forgot the parentheses/brackets. I've added them for you.

            This seems to work well, but you should consider changing the colors and font - black and white is a bit boring!
            thanks. This code is user customizable. They can change the colors at will.

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Originally posted by eragon
              thanks. This code is user customizable. They can change the colors at will.
              Of course, but rather than changing inline, it should be customizable through JavaScript since the overlay should not display if JavaScript is not enabled. In fact, you should consider a drop-in script which would involve the use of document.write after the body tag.

              Comment

              • eragon
                Contributor
                • Mar 2007
                • 431

                #8
                Originally posted by acoder
                Of course, but rather than changing inline, it should be customizable through JavaScript since the overlay should not display if JavaScript is not enabled. In fact, you should consider a drop-in script which would involve the use of document.write after the body tag.
                good idea.... ill change that and post another code. Because with this one, if the user doesn't have javascript, your just going to have an infinitely loading page.

                ill post another version when i get done with a few things.

                Comment

                • katalin
                  New Member
                  • Mar 2009
                  • 2

                  #9
                  Hi, this does not seem to work in IE8, could anyone fix this?
                  LE: I added Adsense code during loading process and after the page is loaded adsense sticks out, does not disappear.
                  LE2: Adsense issue fixed :D
                  Now I only need to make it work in IE8.

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    What happens in IE8? Have you tried other versions of IE?

                    You may want to post in the forum instead of here to get a better response.

                    Comment

                    • katalin
                      New Member
                      • Mar 2009
                      • 2

                      #11
                      Originally posted by acoder
                      What happens in IE8? Have you tried other versions of IE?

                      You may want to post in the forum instead of here to get a better response.
                      In Mozilla, Safari and Chrome everything looks perfect but in IE(tried 6,7 and 8) the preloader is loading in the right of the page, not in the middle, maybe is because position:absolu te, i don't know... can anyone help?

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        Ah right, that's more a CSS problem. Here's one way you could do it. You may also find this useful. Failing that, ask in the HTML/CSS forum.

                        Comment

                        • eragon
                          Contributor
                          • Mar 2007
                          • 431

                          #13
                          Two years it took.

                          But alas, i have returned.

                          And by the way, i have a better code.

                          [CODE=html/javascript]<html>
                          <head>
                          <title>Loadin g Screen</title>
                          <script type="text/javascript">

                          //Hide the body. See, if you have javascript, it would hide, otherwise, it's visible.
                          elem3 = document.getEle mentById("bodyd iv");
                          elem3.style.vis ibility="hidden ";

                          //Show the loading page. Same as above.
                          elem4 = document.getEle mentById("overl ay");
                          elem4.style.vis ibility="visibl e";

                          function overlay() {
                          elem = document.getEle mentById("overl ay");
                          elem.style.visi bility="hidden" ;
                          elem2 = document.getEle mentById("bodyd iv");
                          elem2.style.vis ibility="visibl e";
                          }
                          </script>
                          <style>

                          #overlay {
                          width: 100%;
                          height: 100%;
                          position: absolute;
                          visibility: hidden;

                          /*Background information for the preloading mask:*/
                          background-color:#000000;

                          /*Font color and options:*/
                          color:#CCCCCC;
                          font-family:Georgia, "Times New Roman", Times, serif;
                          font-size:16px;
                          }

                          </style>
                          </head>
                          <body onLoad="overlay ()">
                          <div id="overlay">
                          <table><tr><t d valign="center" height="100%" width="100%">
                          <div align="center">
                          Loading... Please Wait.<!--You can put pretty much whatever here.-->
                          </div>
                          </td></tr></table>
                          </div>

                          <div id="bodydiv" style="visibili ty:visible;">

                          <!-- YOUR BODY CONTENT WOULD GO HERE -->

                          </div>
                          </body>
                          </html>[/CODE]

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            That wouldn't work because on line 7, bodydiv wouldn't have been defined yet.

                            Comment

                            Working...