www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] Tricky Alert Situation / Image loading complete check

Hybrid View

  1. #1
    Join Date
    Sep 2007
    Location
    INDIA
    Posts
    12

    resolved [RESOLVED] Tricky Alert Situation / Image loading complete check

    A script with alert works fine. But when I comment the alert (actually I need to), the browser Mozilla 3 hangs and IE6 cease to function with an error message.

    SCRIPT (With the alert. When the alert is commented, the script fails as stated above):
    Code:
    var myImage = new Image;
    myImage.src = "./images/allback.jpg";
    
    do
    {
         setTimeout('',10);
    } while(!myImage);
    
    var chk= false;
    var i=1;
    
    while(!chk)
    {
         chk= myImage.complete;
         alert(chk+' / '+i);
         i++;
    };
    
    if(chk)
         imload= true;
    IE6 ERROR MESSAGE:

    A script on this page is causing Internet Explorer to run slowly. If it continues to run, your computer may become unresponsive. Do you want to abort the script?

    If 'yes' is chosen the page loads without the execution of the script. Otherwise it hangs.
    I need a way out.

    P.S.: As per the above code, I need to check if an image is completely loaded or not. Any alternative suggestions are welcome. But I'd like to know why the current code fails. Thanks in advance.
    Last edited by archaeocodifix; 06-26-2009 at 06:07 PM.

  2. #2
    Join Date
    Jun 2007
    Posts
    667
    The do-while loop is pointless since myImage is true initially.

    myImage.complete will not change during the execution of the while loop.

    Code:
    var myImage = new Image;
    
    myImage.onload = function(){ /* do something here */ };
    
    myImage.src = "./images/allback.jpg";

  3. #3
    Join Date
    Sep 2007
    Location
    INDIA
    Posts
    12
    Thanks Sterling,
    Youre The One.

    But one thing I'd like to say, I do think myImage.complete changes during the execution of the while loop. During my testing, the loop is executed twice, i.e. I get two alert messages as:
    • false / 1
    • true / 2


    So don't these indicate that myImage.complete changes ?
    Last edited by archaeocodifix; 06-27-2009 at 02:30 AM. Reason: confusion

  4. #4
    Join Date
    Sep 2007
    Location
    INDIA
    Posts
    12
    myImage.onload was not working in Opera. So I modified the code a bit to skip the onload event check in Opera.

    Here is the new code to handle Opera browsers:

    Code:
    var browser= navigator.appName;
    
    if(browser.toLowerCase()!='opera')
    	myImage.onload = function(){ /* do something here */ };
    else
    {
    	/* do something here */
    }
    http://phsoftware.de/Blog/2008/09/im...load-problems/

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles