Do i need to use window. when using alert or settimeout

Hi,

Should I be using window.alert or just alert when using jquery? Does it make any difference?

thanks

It doesn’t make a difference, AFAIK. Using window for anything is just another way of saying “Hey, I need to access something in the global scope.” NOT using window defaults to window (I think). :smile:

HTH,

:slight_smile:

Hi okid,

In JavaScript all global variables are attached to the window object, so if you have jQuery loaded you can refer to it using $ or window.$

There’s really no difference as to whether you prefix your function calls with window. or not, except in situations where you might have a local variable with the same name:

function doSomething() {
    var alert = "Your changes have been saved";
    alert(alert); // Throws a type error
    window.alert(alert); // Works
}

but you really should avoid doing things like that anyway.

I think that you should use window.alert to act as a warning that you are accessing something globally.

Leaving in the window part can also be helpful when you are scanning through the code later on, for it acts as a reminder of what you have done, and serves as inspiration to come back and improve on things to remove the need for it.

So leave it in. It does no harm and can serve a beneficial purpose later.

2 Likes

That is a good reason for using window.alert() instead of just alert(). Makes it easier to find all the calls and remove them before the site goes live.

For the same reason it makes sense to leave the window off the front of setTimeout and setInterval as those have a place in live code and are not just used for quick debugging in antiquated browsers that don’t have a debugger built in.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.