如何在JS变量中保存它?

时间:2014-08-06 11:33:25

标签: javascript

我想知道如何将此输出保存到" var a ="

navigator.plugins.refresh(false);

var numPlugins = navigator.plugins.length;


for (var i = 0; i < numPlugins; i++){
  var plugin = navigator.plugins[i];

  if (plugin) {
    document.write(plugin.name + plugin.description + plugin.filename)
  }
}

1 个答案:

答案 0 :(得分:3)

在循环之外声明a并将其定义为空字符串,然后在结束时将结果附加到它:

navigator.plugins.refresh(false);

var numPlugins = navigator.plugins.length;
var a = '';

for (var i = 0; i < numPlugins; i++){
  var plugin = navigator.plugins[i];

  if (plugin) {
    a += plugin.name + plugin.description + plugin.filename;
  }
}

你可能想要使用一个字符串数组,因为你可以有很多插件:

navigator.plugins.refresh(false);

var numPlugins = navigator.plugins.length;
var a = [];

for (var i = 0; i < numPlugins; i++){
  var plugin = navigator.plugins[i];

  if (plugin) {
    a.push(plugin.name + plugin.description + plugin.filename);
  }
}

编辑如果您需要将a哈希到某些内容:

var hash = yourMd5Function(a);

或者第二个例子:

var b = a.join(','); // "plugin1,plugin2,..." for example
var hash = yourMd5Function(b);