Web Analytics Made Easy -
StatCounter Mystery with variable in IF..ELSE - CodingForum

Announcement

Collapse
No announcement yet.

Mystery with variable in IF..ELSE

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Mystery with variable in IF..ELSE

    Hi friends,

    I have a mystery. Here below a function that loads an image. It happens that the image width and height could not be gathered before executing the openPopImg() function, so I have added a ELSE portion where the pleaseWait() window is popped up, then I do a getImage(imgFleet) again. The problem is that the value of imgFleet in the ELSE portion 'is undefined'. I do not understand why, though it's still a known local variable.


    function getImage(imgFleet)
    {
    var imgX = new Image();
    imgX.src = imgFleet;
    var imgWidth = imgX.width
    var imgHeight = imgX.height;
    var picName = imgFleet;
    if (imgHeight){
    openPopImg(imgFleet,picName,imgWidth,imgHeight);
    }
    else
    {
    pleaseWait();
    setTimeout('getImage(imgFleet)',5000); // it's here that imgFleet is undefined....
    }
    }


    Code below is only for information

    function pleaseWait()
    {
    text = "<html>\n<head>\n<title>Pop Window</title>\n<body bgcolor='red'>\n";
    text += "<center>\n<br>";
    text += "<h2>Please Wait...</h2>";
    text += "<h5>If the image doesn't appear, click again.</h5>";
    text += "</center>\n</body>\n</html>\n";
    var window_width=300;
    var window_height=100;
    var window_left=(screen.availWidth/2)-(window_width/2);
    var window_top=(screen.availHeight/2)-(window_height/2);
    var window_dimensions="height="+window_height+
    ",width="+window_width+
    ",left="+window_left+
    ",top="+window_top
    newWindow = window.open('','newWin',window_dimensions);
    newWindow.document.write(text);
    setTimeout('closeWin(newWindow)',5000); // delay 5 seconds before closing
    }

    function closeWin(newWindow)
    {
    newWindow.close(); // close small window
    }

  • #2
    Try this:

    setTimeout(function(){getImage(imgFleet)},5000);

    -- courtesy of beetle
    hmm... ?

    Comment


    • #3
      What's the details on your openPopImg() function? Also, when writing HTML, you don't have to put your tags on separate lines. It would save script size by removing the \n as they aren't absolutely needed.
      Go to:
      http://www.TailsArchive.net/
      http://xfox.proboards21.com/

      Comment


      • #4
        To friend 'swmr':

        I would like to thank you very much for your help! It did the trick, although I still don't understand why it didn't worked without adding function(){...} into the setTimeout function....

        To friend 'Tails':

        In fact, the details of openPopImg() is irrelevant since it's executed only if the condition is true, while my question was about the false condition.
        Anyway, to satisfy your curiosity, here is the complete "popup.js" function's listing. If you want to try out, please visit my site www.charvi.com and choose 'Belgian Fleet'. It includes the correction presented by our friend 'swmr'.

        // Pop-up Images Script v2.0 - Copyright 1999,2002 Ray Stott, http://www.crays.com/jsc
        // Some additions by Richard Vinck

        var pic = null
        var popImg = null // use this when referring to pop-up image
        var picTitle = null
        var imgCount = 0
        var imgWinName = "popImg"
        var priorPic = new Array()
        var noPic = 0
        var foundit = false

        function pleaseWait()
        {
        text = "<html>\n<head>\n<title>Pop Window</title>\n<body bgcolor='red'>\n";
        text += "<center>\n<br>";
        text += "<h2>Please Wait...</h2>";
        text += "<h5>If the image doesn't appear, click again.</h5>";
        text += "</center>\n</body>\n</html>\n";
        var window_width=300;
        var window_height=100;
        var window_left=(screen.availWidth/2)-(window_width/2);
        var window_top=(screen.availHeight/2)-(window_height/2);
        var window_dimensions="height="+window_height+
        ",width="+window_width+
        ",left="+window_left+
        ",top="+window_top
        newWindow = window.open('','newWin',window_dimensions);
        newWindow.document.write(text);
        setTimeout('closeWin(newWindow)',5000); // delay 5 seconds before closing
        }

        function closeWin(newWindow)
        {
        newWindow.close(); // close small window
        }

        function getImage(imgFleet)
        {
        var imgX = new Image();
        imgX.src = imgFleet;
        var imgWidth = imgX.width
        var imgHeight = imgX.height;
        var picName = imgFleet;
        if (imgHeight){
        openPopImg(imgFleet,picName,imgWidth,imgHeight);
        }
        else
        {
        pleaseWait();
        setTimeout(function(){getImage(imgFleet)},5000);
        }
        }

        function openPopImg(picName, windowTitle, windowWidth, windowHeight)
        {
        closePopImg()
        picTitle = windowTitle
        imgWinName = "popImg" + imgCount++ //unique name for each pop-up window
        popImg = window.open(picName, imgWinName, "toolbar=no, scrollbars=no, resizable=no"
        + ",width=" + (parseInt(windowWidth)+20)
        + ",height=" + (parseInt(windowHeight)+15))
        }

        function closePopImg() // close pop-up window if it is open
        {
        if (navigator.appName != "Microsoft Internet Explorer" || parseInt(navigator.appVersion) >=4){ //do not close if early IE
        if(popImg != null) {
        if(!popImg.closed){
        popImg.close()
        }
        }
        }
        }

        function setStatus(msg)
        {
        status = msg
        return true
        }

        function openPopImg(picName, windowTitle, windowWidth, windowHeight)
        {
        var i = 0
        if(pic == picName && winOpen()){
        popImg.focus()
        } else {
        foundit = false
        for(i=0; i<=noPic; i++){
        if (priorPic[i] == picName) {
        foundit = true
        }
        }
        pic = picName
        closePopImg()
        picTitle = windowTitle
        imgWinName = "popImg" + imgCount++ //unique name for each pop-up window
        if(!foundit){
        priorPic[noPic++] = pic
        }
        popImg = openPopImgWin(imgWinName, windowWidth, windowHeight)
        }
        }

        function winOpen()
        {
        if(popImg != null){
        if(popImg.closed != true) return true; else return false
        } else {
        return false
        }
        }

        function openPopImgWin(imgWinName, windowWidth, windowHeight) // Use this function to control placement of pop-up window (Netscape 4+ and IE4+)
        {
        var leftX = (screen.width - windowWidth) / 2 ___// distance of window's leftside from left of screen
        var topY = ((screen.height - windowHeight) / 2)-32 __// distance of window's top side from top of screen
        var winFeatures = "toolbar=no,scrollbars=no,resizable=no,width=" + windowWidth + ",height=" + windowHeight
        if (leftX>0){
        winFeatures += ",screenX=" + leftX + ",left=" + leftX
        + ",screenY=" + topY + ",top=" + topY
        }
        return window.open("pimg.htm", imgWinName, winFeatures)
        }

        Comment


        • #5
          It did not work without the function(){...} because anything enclosed in '...' is a string and is not evaluated.

          so, when you say 'getImage(imgFleet)' its really calling getImage with an undefined value of imgFleet because at this point imgFleet is not available.

          On the other hand when you say function(){getImage(imgFleet)}, the variable imgFleet is replaced with the object as it is evaluated, so it works..

          Hope that helped.
          Nobody is Perfect. I am Nobody.

          Comment


          • #6
            To friend 'Unit':

            Yes, thank you very much, it helped me to understand much better.
            So in fact, I had to write the code

            'getImage('+imgFleet+')'

            I have not tested it yet, but it should follow the correct logic if I understood you well.

            But then, how does it come that the code
            setTimeout('closeWin(newWindow)',5000); // delay 5 seconds before closing
            in the function pleaseWait() is evaluated???

            Comment


            • #7
              I think I overlooked it a little. The src of your preloaded image must be a filename. If you're pointing to an object, it will expect the object to be a string pointing to a valid file name.

              imgX.src = imgFleet;

              imgFleet would have to be something like 'file.gif'. Also remember that imgX is an object, but you'll have to get the image url from imgX.src
              Go to:
              http://www.TailsArchive.net/
              http://xfox.proboards21.com/

              Comment


              • #8
                To friend 'Tails':

                Yes, it's an image. The source line in "v1.htm" is as follows:

                <td align="center" bgcolor="#008000"><a href="v1.htm" onClick="getImage('1047.jpg'); return false">1047</a></td>

                The particularity of the display in a popup window is that it becomes a background image, so the popup has no white border. And for that reason I needed to obtain the width and height of the image that will be popped up because each image has its own size. This is accomplished in the pimg.htm file, here below is the full source:

                <HTML>
                <HEAD>
                <TITLE>Loading</TITLE>
                <SCRIPT Language="JavaScript">
                function fabricatePage(){
                var htmlStr =
                '<HTML><HEAD><TITLE>' + window.opener.picTitle + '</TITLE></HEAD>'
                + '<BODY background="' + window.opener.pic + '"></BODY></HTML>'
                document.open()
                document.write(htmlStr)
                document.close()
                }
                </SCRIPT>
                </HEAD>

                <BODY bgcolor="#CCCCCC" onLoad="fabricatePage()">
                <SCRIPT Language="JavaScript">
                if(!window.opener.foundit){
                document.write("Loading Picture...<BR>")
                document.write('<img src="' + window.opener.pic + '" width=100% height=80%>')
                document.close()
                }
                </SCRIPT>
                </BODY>
                </HTML>

                Comment


                • #9
                  To Richard2003:

                  You may want to check out the Popup image viewer created by beetle. It has a smarter way of flashing "Loading..." status message by waiting for the image to load. Check out my sig.
                  Glenn
                  vBulletin Mods That Rock!

                  Comment

                  Working...
                  X