我正在尝试从命令行传递参数并选中或取消选中本地网站上的复选框。如果我用 true
或 false
替换 returnVars
函数中的 system.args[4]
,它就可以工作,但我从命令行传递的参数不会影响该复选框。
var returnVars = function(){
if (system.args.length > 4) {
console.log(system.args[4]); // this is getting the correct value from the args
return system.args[4];
}
};
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
phantom.exit(1);
} else {
var returnable = page.evaluate(function(r,s) {
return document.getElementById(r).checked = s;
}, 'returnable', returnVars());
window.setTimeout(function () {
page.render(output);
phantom.exit();
}, 200);
}
});
我正在使用 rasterize.js 示例,并将缩放选项替换为我自己的。我这样调用它:
phantomjs rasterize.js mywebsite.com c:\foo.pdf "letter" false
最佳答案
您将字符串“false”
传递给evaluate
并将checked
属性设置为此字符串。然后,该字符串将被计算为 true
,因为 !!"false"=== true
和 !!"true"=== true
。如果希望您现在就看到问题。
这很容易解决。由于您将 JavaScript 代码 (false
) 作为字符串传递,因此您需要执行该字符串,使其再次成为 JavaScript 代码:
document.getElementById(r).checked = eval(s);
eval
通常是邪恶的,但由于这是您的本地脚本,因此您无法破坏安全性。
关于javascript - 将参数从 PhantomJS 命令行传递到页面评估以更改复选框状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26981680/