Jacob
Jacob

Reputation: 1

Javascript Only Works with alert, why?

The statement I'm concerned about in the following statement is fancybox = 1; That needs to happen if my maxWidth for any of the temporary images I create is over 480.

A little background on the html this interacts with: I have a link wrapped around an image.
The image is a resized version, and the link's href is to the original, unsized image.
There is a series of these link wrapped images
A div, addon-large-image, wraps the whole thing.

For some reason, this code works if I have 'alert(m);' included. I correctly end up in side my final if statement (in this case I do have images wider then 480) and the last alert I get is "Triggered". However, if I comment out 'alert(m);', and change nothing else, 'alert("Triggered");' fails to fire, showing me that I have not, in fact, entered my last conditional.

Any thoughts on what I'm doing wrong here? I have a programming background in Java, but I'm relatively new to Jquery in any heavy sense, so I'm guessing I have a syntax problem of some sort that 'alert(m);' is sort of incidentally fixing.

'tallest' is irrelevant in the scope of my problem, it does what it's supposed to correctly, is used elsewhere, and existed before I implemented maxWidth.

    var tallest = 0;
    var tempImg = new Image();
    var tempSrc = "";
    var maxWidth = 0;

    // Finds the tallest image in the set.
    $("#addon-large-image img").each(function () {
        var n = $(this).attr("height");
        if (tallest < n) {
            tallest = n;
        }

        tempSrc = $(this).parent().attr("href");
        $(tempImg).attr("src", tempSrc);

        var m = $(tempImg).attr("width");

        alert(m);

        if (maxWidth < m) {
            maxWidth = m;
        }
    });

    if (maxWidth > 480) {
        fancybox = 1;
        alert("Triggered");
    }

Upvotes: 0

Views: 3883

Answers (2)

Ali Gh
Ali Gh

Reputation: 700

Did you use Ajax Request ?

if YES then put you code that didnt work at the end of

AjaxObject.onreadystatechange = function () {
       if (AjaxObject.readyState == 4) {
           .
           .
           .
           .
           .

      // PUT HERE
        }
   }

then if No

use this :

function testName()
{
$(document).ready(function(){
    var tallest = 0;
    var tempImg = new Image();
    var tempSrc = "";
    var maxWidth = 0;

    // Finds the tallest image in the set.
    $("#addon-large-image img").each(function () {
        var n = $(this).attr("height");
        if (tallest < n) {
            tallest = n;
        }

        tempSrc = $(this).parent().attr("href");
        $(tempImg).attr("src", tempSrc);

        var m = $(tempImg).attr("width");

        alert(m);

        if (maxWidth < m) {
            maxWidth = m;
        }
    });
});
return true;
}

hope helps you buddy

Upvotes: 0

nickytonline
nickytonline

Reputation: 6981

Looks like something isn't fully loaded in your script yet. Try running this in a jQuery document.ready and see if it works.

Upvotes: 1

Related Questions