0

With some help of the people here, I've been able to get the original width of an image with the following code:

var img = new Image();

        img.onload = function () {
            var W2 = this.width;
            alert(W2);
        }

        img.src = document.getElementById('imageViewerImg').src;

Now that I have the width of an image, I need a conditional statement to modify the CSS if an image is wider than 700px.

My code to do this looks like this:

$(document).ready(function(){
        var img = new Image();

        img.onload = function () {
            var W2 = this.width;
        }

        img.src = document.getElementById('imageViewerImg').src;

        if($W2 > 700) {
            $("#photoHolder").css({"vertical-align":"none","text-align:":"none"});
        }
    });

But this won't work. I've tried to add an alert box in the conditional statement, so if an image happens to be wider than 700px, it should appear and notify. This alert box won't show up either. Even if I put the alert box outside the if statement and just put in below, it won't work, like this:

$(document).ready(function(){
            var img = new Image();

            img.onload = function () {
                var W2 = this.width;
            }

            img.src = document.getElementById('imageViewerImg').src;

            alert(W2)
        });

Where should I put my if statement in this code to make it work?

2

4 Answers 4

1

As you're using jQuery, keep everything like this. Don't mix up with regular Javascript DOM manipulation. And your variable needs to be defined in a scope which it's visible across the code.

$(function () {
    var img = new Image();
    var W2 = 0;

    $(img).on("load", function () {
        W2 = $(this).width;
    });

    $(img).attr("src", $("#imageViewerImg").attr("src");

    if (W2 > 700) {
        $("#photoHolder").css({"vertical-align":"none","text-align:":"none"});
    }
});
Sign up to request clarification or add additional context in comments.

Comments

1

Move your if logic inside of the onload for the image, like this:

$(document).ready(function(){
    var img = new Image();

    img.onload = function () {
        var W2 = this.width;
        if(W2 > 700) {
            $("#photoHolder").css({"vertical-align":"none","text-align:":"none"});
        }
    }

    img.src = document.getElementById('imageViewerImg').src;
});

Note: JavaScript uses nested scope, so when you defined W2 inside of image.onload it was not available where you originally had the if logic.

2 Comments

This won't work. I've added an alert box in the if statement, but it won't show up. I keep having the same problem.
Removed $ in front of W2, answer updated. You do not need to reference W2 as a jQuery wrapped set, because it is not one.
0

You have a scoping problem:

 $(document).ready(function(){
        var img = new Image();
        var W2;
        img.onload = function () {
            W2 = this.width;
        }

        img.src = document.getElementById('imageViewerImg').src;

        alert(W2);

        if($W2 > 700) {
          $("#photoHolder").css({"vertical-align":"none","text-align:":"none"});
        }
    });

You need to define W2 where your if statement can access it.

Or

Better yet, you could place your if statement in the onload event handler:

$(document).ready(function(){
    var img = new Image();

    img.onload = function () {
        var W2 = this.width;
        if($W2 > 700) {
          $("#photoHolder").css({"vertical-align":"none","text-align:":"none"});
        }
    }

    img.src = document.getElementById('imageViewerImg').src;
});

4 Comments

I don't really know what a scoping problem is, but when I copy and paste your code, the alert box shows a message 'undefined' and the result I need doesn't appear, I think you're showing the problem to me. Could you help me with solving the scoping problem?
If you define W2 inside the onload event handler the outer context cant access it. I edited my answer, place your code in the onload event handler.
@user2611052 Im not sure what you are trying to do so I am not surprised it doesn't work. You need to understand the scoping problem you have with your code as well as the order of operations issue you have expecting the 'onload' event handler to execute where you have it placed.
All variables have scope. The scope is the region of the program for which the variable is declared. JavaScript has two levels of scope, global and local. A global variable is one that is defined everywhere in your script, as well as, by extension, everywhere in the document containing the script. All predefined variables and objects are global in scope, as are implicitly declared variabled and variables that are defined outside of functions. A local variable is one that is local to a function. In other words, it only exists within the function for the duration of the function.
0

Since you are using jQuery, you can employ the width() function without needing to define a new Image. For example:

<img src="http://bunkerhill.com/images/bh_commander.jpg" alt="Battle Cry"/>
$(function() {
    var w2=$("img").width();
    alert(w2);
    if (parseInt(w2)>700) {
       //do something
    }
});

jsfiddle

2 Comments

I've added your code, and yes, it works on JSfiddle, but when I put it on my website, inside the document ready function, the alert box shows the message '0'.
How are you referencing the image? Within the <body> tag?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.