What does "typeof window == 'undefined' " mean?

ritaaudi
Tera Contributor

Hi: We have the following script in our 'Resolve Now' UI action that is called every time the user clicks the 'Resolve Now' button:

if (typeof window == 'undefined') {

  serverResolve();

}

I am wondering if something is wrong with the code. What is window == 'undefined'? Is it always 'undefined'? When is it 'defined'?

Thanks for your help!

Rita

1 ACCEPTED SOLUTION

Hi Rita,  



if(typeof window == 'undefined') means



1.Code that runs without 'onclick'.


2.Ensure call to server-side function with no browser errors


View solution in original post

10 REPLIES 10

Vikas-Malhotra
Mega Guru

Hi Rita,



It's to test if the script is being run in a web-browser or not. In a webpage, there are several intrinsic objects, such as window, other environments (like Node.js) won't have window but might have other objects like console (well, console now exists in most browsers now, but it wasn't originally).



Thanks and Regards,


Vikas Malhotra


Hi Vikas and thank for your prompt response. So is it correct to be getting   typeof window == 'undefined' if I'm using a web browser, which I am?


Regards, Rita


Hi Rita,



Yes, there may be a case when you get typeof window == 'undefined' if you are using a web browser like in case of web workers.


Hope this is helpful.



Thanks and Regards,


Vikas Malhotra


Hi Vikas: It appears that typeof window == 'undefined' always executes in UI Actions. So I am assuming that this is server side code that always executes regardless of browser. Thanks for your help. Rita