I have a function that has a values inserted into variables in another function.
Initialise:
When the window resizes, it fires the resizeImage() function.
$(window).resize(function () {
resizeImage();
});
Resize Function:
This function checks every element with the image-resize class and add readjust the SRC for the image by changing the width value (dynamic from scene7).
The problem is that element.attr('src', newSrc); newSrc doesn't exist as its stored in sizingMethod().
Is there anyway of grabbing the variable from sizingMethod() function and placing it within the element.attr('src', newSrc);.
function resizeImage() {
$('.image-resize').each(function () {
var element = $(this), src = $(this).attr('src'), regx = /wid=\d+(\.\d)*/g, currentWidth, newWidth, newSrc;
var attrElement = $(this), attrSrc = $(this).attr('data-zoom-image'), attrRegex = /wid=\d+(\.\d)*/g, attrCurrentWidth, attrNewWidth, attrNewSrc;
if ($(window).width() > 1824) {
sizingMethod(src, regx, currentWidth, newWidth, newSrc, '2000');
} else if ($(window).width() <= 1824 && $(window).width() > 1366) {
sizingMethod(src, regx, currentWidth, newWidth, newSrc, '1824');
}
element.attr('src', newSrc);
});
};
Sizing Function:
function sizingMethod(sSrc, sRegex, sCurrentW, sNewW, sNewSrc, sNewWidth){
sCurrentW = sSrc.match(sRegex);
sNewW = 'wid=' + sNewWidth;
sNewSrc = sSrc.replace(sCurrentW, sNewW);
textWidth = sNewW.replace('wid=', '');
$(".dsply-screen-size").text($(window).width());
$(".dsply-image-size").text(textWidth);
}
Variable defined inside the method: The variables that are defined inside the methods can be accessed within that method only by simply using the variable name. Example – var_name. If you want to use that variable outside the method or class, you have to declared that variable as a global.
You can access global variables in Python both inside and outside the function.
You can access such variables inside and outside of a function, as they have global scope. The variable x in the code above was declared outside a function: x = 10 . Using the showX() function, we were still able to access x because it was declared in a global scope.
When you use a primitive, what you send is the value not the reference, so the sNewSrc != newSrc, so any changes you do to sNewSrc that variable will not change newSrc.
You can fix your code using:
return: returning the value in the function sizingMethod:
if ($(window).width() > 1824) {
newSrc = sizingMethod(src, regx, currentWidth, newWidth, '2000');
} else if ($(window).width() <= 1824 && $(window).width() > 1366) {
newSrc = sizingMethod(src, regx, currentWidth, newWidth, '1824');
}
Sizing Function:
function sizingMethod(sSrc, sRegex, sCurrentW, sNewW, sNewWidth){
sCurrentW = sSrc.match(sRegex);
sNewW = 'wid=' + sNewWidth;
var sNewSrc = sSrc.replace(sCurrentW, sNewW);
textWidth = sNewW.replace('wid=', '');
$(".dsply-screen-size").text($(window).width());
$(".dsply-image-size").text(textWidth);
return sNewSrc;
}
closure: if declare sizingMethod inside the anonymous function you have access to the variable newSrc and other variables as well.
function resizeImage() {
$('.image-resize').each(function () {
var element = $(this), src = $(this).attr('src'), regx = /wid=\d+(\.\d)*/g, currentWidth, newWidth, newSrc;
var attrElement = $(this), attrSrc = $(this).attr('data-zoom-image'), attrRegex = /wid=\d+(\.\d)*/g, attrCurrentWidth, attrNewWidth, attrNewSrc;
if ($(window).width() > 1824) {
sizingMethod('2000');
} else if ($(window).width() <= 1824 && $(window).width() > 1366) {
sizingMethod('1824');
}
element.attr('src', newSrc);
function sizingMethod(sNewWidth){
currentWidth = src.match(regx);
var sNewW = 'wid=' + sNewWidth;
newSrc = sSrc.replace(currentWidth, sNewW);
textWidth = sNewW.replace('wid=', '');
$(".dsply-screen-size").text($(window).width());
$(".dsply-image-size").text(textWidth);
}
});
};
object: if you use an object/array instead of a primitive as an argument
function resizeImage() {
$('.image-resize').each(function () {
var element = $(this), src = $(this).attr('src'), regx = /wid=\d+(\.\d)*/g, currentWidth, newWidth, newSrc = {};
var attrElement = $(this), attrSrc = $(this).attr('data-zoom-image'), attrRegex = /wid=\d+(\.\d)*/g, attrCurrentWidth, attrNewWidth, attrNewSrc;
if ($(window).width() > 1824) {
sizingMethod(src, regx, currentWidth, newWidth, newSrc, '2000');
} else if ($(window).width() <= 1824 && $(window).width() > 1366) {
sizingMethod(src, regx, currentWidth, newWidth, newSrc, '1824');
}
element.attr('src', newSrc.src);
});
};
Sizing Function:
function sizingMethod(sSrc, sRegex, sCurrentW, sNewW, sNewSrc, sNewWidth){
sCurrentW = sSrc.match(sRegex);
sNewW = 'wid=' + sNewWidth;
sNewSrc.src = sSrc.replace(sCurrentW, sNewW);
textWidth = sNewW.replace('wid=', '');
$(".dsply-screen-size").text($(window).width());
$(".dsply-image-size").text(textWidth);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With