本文介绍了如何在Javascript中设置全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在想设置一个全局变量.我知道这应该是一件坏事,但是当我精通此功能并且使用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.

我正在使用ajax调用,需要将返回服务器值存储在全局变量中.因此,如果我有:var = MyglobalVar = 'varFromMyServer';,如何准确设置此设置,以便可以从脚本中的任意位置访问它?

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 is a global container for browsers.

window.myGlobalVar = 'varFromMyServer';

这篇关于如何在Javascript中设置全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 09:57