Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Mar 2004
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Preloading problem with Netscape

    I've got a weird preloading problem with Netscape. I use the following code to preload an image and store its dimensions so I can open a popup window that matches its size. The problem is the first time i click on a link to run the script it returns the height and width of 0. If I click again it retrieves the correct image size. In IE it seems to work fine. Whats going on?

    var iObj = new Image(); iObj.src = iurl; //load image
    var iW = iObj.width; var iH = iObj.height; // store image size
    if(iW<100){iW = 100}; if(iH<100){iH = 100};
    var iWin = window.open('','','height='+iH+',width='+iW+',top=100,left=100'); //open window to correct size

    Here the actual script:
    http://mydesignbiz.com/projects/avan...age_resize.htm
    Last edited by javaPete; 03-18-2004 at 03:50 AM.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    You must wait for the image to load completely before getting its dimensions. The image has onload event that triggers when the image has completely loaded. You may want to check the "Popup image viewer" link in my sig. I think it's what you need.

  • #3
    New Coder
    Join Date
    Mar 2004
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thx glenngv i first used a script similar to the one you pointed me to but i didn't like the effect where the window is displayed and then you see it resized. Someone else showed me the current script I'm using. I was impressed by how much simpler the code was and how it sized the window before displaying (much more elegant method). Only when i put it up on a test server did i discover the preload problem.

    Its funny when you look at someone elses script you say god thats a real ugly, long way of doing a simple task. Only after you've tried unsucessfully to streamline it you figure out why the programmer wrote it that way.

    One last thing, I found it interesting the way you were able to resize the window correctly. In other popup scripts they use Navigator/IE test to compensate for the different way each browser measures window size. I'm curious how your script works without such a test?

    Another quick thought, I've don't see resize script used on most mainstream sites. They must either have a serverside solution or hardcode their popup window size.
    Last edited by javaPete; 03-18-2004 at 06:19 AM.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    If you really want to use the original script, then you should modify it to put an onload handler.
    Code:
    function ResizedWinImage(iurl){ 
      var iObj = new Image(); 
      //assign onload handler before setting the src
      iObj.onload = function(){
        var iW=this.width;var iH=this.height;
        if(iW<100){iW = 100}; if(iH<100){iH = 100}; //insure the window in no smaller than 100x100
        var iWin = window.open('', '', 'height='+iH+',width='+iW+',top=100,left=100, location=no, menubar=no, status=no, toolbar=no, scrollbars=no, resizable=no'); //open new window and set its features
        with(iWin.document){ //shortens writeln method
    	//write html to new window to:  insert image and script to close window
            writeln('<html><head><title></title><style>body{margin:0px;}</style></head>');
            writeln('<body onload="self.focus()" onblur="self.close()">');
            writeln('<img src='+this.src+'>');
            writeln('</body></html>');
            close(); //tell browswer were finished writing html to window
        }
    
      } 
      iObj.src = iurl; //create image object with url passed in
    }
    The problem with this way is that if the image's size is big and takes a while to load, the user would see no window popping up and would thought that nothing is working. Unlike in my suggested script in my sig that displays a "Loading..." message as the image is being loaded.


  •  

    Posting Permissions

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