- 我在名为 validate 的函数中声明了一个名为 cont 的局部变量。
- 我正在从内部调用一个函数过程验证。
- 我将字符串“cont”作为验证函数的参数发送。
- 在使用字符串'cont'的过程函数中,我想访问javascript局部变量的值,如window ['cont']。但我不确定。
- 我想做的是尝试访问 php 中的 $GLOBALS 或 $$ 之类的变量。
这是我所做的一个例子。
<script>
function process(str)
{
alert(window[str]);
}
function validate()
{
var cont='once there lived a king named midas';
process('cont')
}
validate();
</script>
原因是我将大多数表单作为 ajax。我不想发出这样的请求字符串。
var param = "command=insert&content=" + encodeURIComponent(cont);
我想这样做。
var param = makeParam('command,[insert],content,(cont)');
我在 makeparam 中所做的是我使用正则表达式来提取键值对。所以我从 (cont) 得到字符串 cont 并将其替换为窗口变量,如 window[cont]。cont 将有字符串'cont'。
那么我们如何通过使用变量名作为字符串来获取变量的内容呢?
所以我正在寻找相当于 php 的 $$ 的 javascript
已编辑
我提取内部(cont)的cont的一部分代码,这意味着我想要()之间的字符串内容。
nxt = str[i+1].match(/\((.*)\)$/)
if(nxt)param += '=' + encodeURIComponent(window[nxt[1]]);
参数的内容是
"command=insert&content=once there lived a king"
// assume that once there lived a king is encoded
编辑。笔记2。
经过几个回复后,我正在编辑代码以添加此内容。
我正在尝试在 php 中做类似 $GLOBALS 的操作。
我还没有尝试过 $GLOBALS 是否也会包含局部变量。
并了解到本地范围不会进入 $GLOBALS。
阅读 Felix King 的更新后更新。
我想使用一个函数来构建一个尽可能简单的查询字符串。像下面这样。
var param = makeParam('command,insert,/title/,/keywords/,/description/,mode,[1],fckcontent,(cont)');
// if it is a text without // or () then the it is a straight key value pair. so i will do comment=insert.
//if it is /title/ then the key is title and its value is an input elements value with id as title so title=getElementById('title')
//if it is mode,[1] then mode is the key and 1 is its direct value//
//if it is fckcontent,(cont) then fckcontent is the key and cont is a javascript local variable which will contain html content from a WYSIWYG editor.
// a sample result will be
var param = "command=insert&keywords=somekeywords&description=somedescription&mode=1&fckcontent=<p>once there lived a king<p>
然后 casablanca 声明 $GlOBALS 将不包含局部范围变量,这与 javascript 中的方式相同。这是正确的。