Try either adding the the variables to the global scope. For example:
// initialise variables in the global scope
var oldWidth, currentWidth;
onResize = function() {
oldWidth=currentWidth;
currentWidth=$('.myDiv').width();
console.log('The value changed from '+oldWidth+' to '+currentWidth)
}
$(window).bind('resize', onResize);
$(window).load(function () {
// set initial values on window load
oldWidth = currentWidth = $('.myDiv').width();
});
A word of caution though. It is probably worth looking at throttling your resize events (some browsers trigger resizes very quickly and this can cause some to crash if you're doing particularly heavy lifting on the front end). Have a look at something like Ben Almans Throttle/Debounce jQuery plugin.
FURTHER ANSWER
It looks like you might be trying to target just specific browser widths in javascript. I would suggest something like the following, that will both trigger events and allow for a global checkable variable (screensizes are based off bootstrap's default responsive css)
$(function(){
onResize = function() {
if($(window).width() < 768 && !window.screenSizes.isMobile){
window.screenSizes = {
"isMobile" : true,
"isTablet" : false,
"isSmallDesktop" : false,
"isLargeDesktop" : false
};
// fire event for change to mobile
$(window).trigger('toMobile');
console.log($(window).width());
console.log(window.screenSizes);
}
if($(window).width() >= 768 && $(window).width() < 992 && !window.screenSizes.isTablet){
window.screenSizes = {
"isMobile" : false,
"isTablet" : true,
"isSmallDesktop" : false,
"isLargeDesktop" : false
};
// fire event for change to tablet
$(window).trigger('toTablet');
console.log($(window).width());
console.log(window.screenSizes);
}
if($(window).width() >= 992 && $(window).width() < 1200 && !window.screenSizes.isSmallDesktop){
window.screenSizes = {
"isMobile" : false,
"isTablet" : false,
"isSmallDesktop" : true,
"isLargeDesktop" : false
};
// fire event for change to small desktop
$(window).trigger('toSmallDesktop');
console.log($(window).width());
console.log(window.screenSizes);
}
if($(window).width() >= 1200 && !window.screenSizes.isLargeDesktop){
window.screenSizes = {
"isMobile" : false,
"isTablet" : false,
"isSmallDesktop" : false,
"isLargeDesktop" : true
};
// fire event for change to large desktop
$(window).trigger('toLargeDesktop');
console.log($(window).width());
console.log(window.screenSizes);
}
}
// example check inside other function
onSomethingElse = function (){
if(window.screenSize.isMobile){
// do somethingelse specifically for mobile
}
}
function attachEvents (){
// Ben Alman's throttler to stop function spam during resize
// benalman.com/projects/jquery-throttle-debounce-plugin/
$(window).resize( $.throttle(350, onResize) );
// example function triggered when go to mobile
$(window).on('toMobile', function (){
// unload slideshow etc.
});
}
function initialize(){
// initialise variables in the global scope
window.screenSizes = {
"isMobile" : false,
"isTablet" : false,
"isSmallDesktop" : false,
"isLargeDesktop" : false
};
// bind events to elements
attachEvents();
// trigger initialization of resize variables
onResize();
}
initialize();
});