Page 1 of 3 123 LastLast
Results 1 to 10 of 23

Thread: Loading Image

  1. #1
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default Loading Image

    I made a script that displays a very small image when the page is loading, and makes it disapeer once finished loading: (http://mburt.mb.funpic.org/loading.htm)

    Code:
    <html>
    <head>
    <script type="text/javascript">
    onload = function() {
    var obj = document.getElementById("img")
    if (!document.readyState) {
    	obj.style.display = "block"
    	}
    else {
    	obj.style.display = "none"
    	}
    }
    </script>
    </head>
    <body>
    <div align="center" id="img" style="display:none">
    <img src="generater.gif">
    </div>
    </body>
    </html>
    The problem is that it only works in Internet Explorer. I think it may have to do with document.readyState. So if anyone could help me, a great thanks
    - Mike

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Heh. That image is funny. Have you seen the mac startup screen? It's similar. Don't worry... not similar in a copyright violation kinda way... just funny.

    As for the problem, you could just use onLoad....
    <body onLoad="stopFunction();">
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by mburt
    Code:
    onload = function() {
    var obj = document.getElementById("img")
    if (!document.readyState) {
    	obj.style.display = "block"
    	}
    else {
    	obj.style.display = "none"
    	}
    }
    Code:
    if (document.getElementById)
        onload = function() {
            var element;
    
            if ((element = document.getElementById('img')) && element.style)
                element.style.display = '';
        };
    <div align="center" ...
    Use CSS to position contents of the div element (or the element itself). I presume you're after the text-align property, here.

    <img src="generater.gif">
    No alt attribute?

    The problem is that it only works in Internet Explorer. I think it may have to do with document.readyState.
    Yes, it's a proprietary property, and I don't see why it's relevant here: the value of that property should have changed long before the onload event would have fired.

    Mike

  4. #4
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Quote Originally Posted by mwinter
    Code:
    if (document.getElementById)
        onload = function() {
            var element;
    
            if ((element = document.getElementById('img')) && element.style)
                element.style.display = '';
        };
    I'm not sure how to apply this to want I want to do. In fact, I can't even see what relevence this has compared to my script. In short; How can I apply it to my script?
    - Mike

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    It's never a good idea to use a tag name as an id.

    Also:

    document.readyState

    is a string value (where supported, in IE and Opera, not in FF, it will always return true), not a boolean as your code seems to imply.

    You might want to use the image tag's onload event to test to see if the image object's complete property is boolean and if so, if it is true or not.

    Mike's (mwinter's) code is meant as an entirely different approach to the matter. Did you try it?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    It doesn't work...:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    if (document.getElementById)
        onload = function() {
            var element;
    
            if ((element = document.getElementById('img')) && element.style)
                element.style.display = '';
        };
    </script>
    </head>
    <body>
    <div align="center" id="img">
    <img src="generater.gif">
    </div>
    </body>
    </html>
    - Mike

  7. #7
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I had reimplemented what you wrote, albeit with feature detection, a defensive style, and omitting the readyState property check. It wasn't until a few hours ago (whilst I was eating dinner) that I realised that you were doing things backwards: hiding the image whilst the document was loading, and showing it once it had completed.

    In a script element in the document head:
    Code:
    onload = function() {
        var image = document.images.loading;
    
        if (image.style) image.style.display = 'none';
    };
    In a script element in the document body:
    Code:
    document.write('<img id="loading" name="loading" alt="Loading..." src="generater.gif">');
    There's no point in displaying the image unless scripting is enabled. Position it using CSS using the id attribute value.

    Mike
    Last edited by mwinter; 09-27-2006 at 08:35 PM.

  8. #8
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Ah I see. I saw the blank value for the img's style property and kind of scratched my head before. Thanks again mwinter
    - Mike

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Well, that would make the element always (by default) display:block. If you were to do it like so:

    Code:
    <html>
    <head>
    <style type="text/css">
    #iload {
    display:none;
    }
    <script type="text/javascript">
    function isloaded(){
            var element;
    
            if ((element = document.getElementById('iload')) && element.style)
                element.style.display = element.style.display=='block'? '' : 'block';
        }
    if (document.getElementById)
        onload = isloaded;
    </script>
    </head>
    <body>
    <div align="center" id="iload">
    <img src="generater.gif">
    </div>
    <script type="text/javascript">
    isloaded();
    </script>
    </body>
    </html>
    Then the element wouldn't ever be displayed in non-javascript enabled browsers and those not supporting getElementById. In javascript enabled browsers that support getElementById, it would be displayed only while the page is loading.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    You forgot the </style> after the style tag
    - Mike

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •