1

How can i call a simple function in Javascript, for example:

function doSmt() {
   alert("hello world")
}

whenever the user is going to resize the window in any way. So if he for example just resize it with the mouse, or if he zoom into or out the website.

I already tried:

window.onresize = doSmt()

as it stands on some websites, but that doesnt work.

0

2 Answers 2

5

You need to pass the function itself, not call it:

window.onresize = doSet;

Sign up to request clarification or add additional context in comments.

Comments

1

You could do it in two ways:

The first one is almost the same as you already have, but you're calling the function instead of binding it:

window.onresize = doSmt;

The second way allows you to get the event as well:

window.onresize = function ( event ) {
    doSmt();
}

Extra

User Silviu Burcea added the useful information that when your doSmt function accepts parameters it can also use the event parameter:

function doSmt(event) {
    alert("Hello world");
}

window.onresize = doSmt;

2 Comments

If your function, e.g doSmt, has an event parameter, you can catch the event anyway: const doSmt = (evt) => { ... }; window.onresize = doSmt;
@SilviuBurcea Thanks, added that information, be it in the style of the question posted.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.