简体   繁体   中英

How do I set a global variable in Javascript?

I want to set a global variable for right now. I know this is supposed to be a bad thing but when I get this functionality hammered down and a little better with javascript in general, I'll come back to this piece of code and refactor it.

I'm using an ajax call and need the return server value to be stored in a global var. So, if I had: var = MyglobalVar = 'varFromMyServer'; , how exactly do I set this up so I can access it from wherever I want in my script?

window是浏览器的全局容器。

window.myGlobalVar = 'varFromMyServer';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM