I haven't worked very much on front-end jQuery framework, so I would like to know how can i check the width of windows on load and call a function & how to call that same function again when size of window changes.
Suppose I want to call a function below 600px of window size on page load
if(windows.size < 600px)
{
//call a function
}
How to achieve this?
You can use $(window).width()
to check the current width of the screen using jQuery and fire function's which need
if($(window).width() < 600) //don't need to write px in the end , only numbers
{
//call a function
}
Suppose you need to call a function CheckWidth()
on page load
and whenever window size changes, you have to write code like this
function CheckWidth(){
//do something if window size is belo 600
if($(window).width() < 600)
{
//code
}
else
{
//else do this
}
}
//call function on page load
CheckWidth();
//call function whenever windows size changes
$(window).on('resize',function(){
CheckWidth();
})
That's it, you are done.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly