I bine the load en the resize function in one.
$(window).on("resize", function () {
if (window.innerWidth < 700) {
alert('hello');
}
}).resize();
But I am looking for a code in plain JavaScript (without jQuery).
How can I create this?
I bine the load en the resize function in one.
$(window).on("resize", function () {
if (window.innerWidth < 700) {
alert('hello');
}
}).resize();
But I am looking for a code in plain JavaScript (without jQuery).
How can I create this?
Share Improve this question edited Oct 30, 2016 at 11:59 ROMANIA_engineer 56.7k30 gold badges209 silver badges205 bronze badges asked Oct 30, 2016 at 11:56 JoppiebJoppieb 391 gold badge1 silver badge7 bronze badges 2-
You're after setting up an event listener for
DOMContentLoaded
andresize
. Check out developer.mozilla/en-US/docs/Web/API/EventTarget/… for details. – JVDL Commented Oct 30, 2016 at 12:02 - You're not bining Load and Resize but DOM ready and Resize (at least from what you provided) – Roko C. Buljan Commented Oct 30, 2016 at 12:02
1 Answer
Reset to default 7You can do this by adding event listener and calling a function on resize:
window.addEventListener("resize", onResizeFunction);
function onResizeFunction (e){
//do whatever you want to do on resize event
}
Same thing is for onLoad event:
window.addEventListener("load", onLoadFunction);
function onLoadFunction(e){
//do the magic you want
}
If you want to trigger function on resize, when the window loads
window.addEventListener("load", onLoadFunction);
function onLoadFunction(e){
//do the magic you want
onResizeFunction();// if you want to trigger resize function immediately, call it
window.addEventListener("resize", onResizeFunction);
}
function onResizeFunction (e){
//do whatever you want to do on resize event
}