javascript中的代码未在eval中执行

javascript中的代码未在eval中执行,javascript,eval,Javascript,Eval,我知道“eval是邪恶的”,但在salesforce中,标准功能是使用eval在单击按钮时执行脚本,此eval脚本可在页面上找到。现在,我以某种方式获取脚本并从中提取评估部分。但由于某些原因,我无法执行该eval字符串 var accountId; var functionName = j$("[name=click_me]").attr('onclick'); __fromScript=true; var temp = j$("[name=click_me]:f

我知道“eval是邪恶的”,但在salesforce中,标准功能是使用eval在单击按钮时执行脚本,此eval脚本可在页面上找到。现在,我以某种方式获取脚本并从中提取评估部分。但由于某些原因,我无法执行该eval字符串

    var accountId;
    var functionName = j$("[name=click_me]").attr('onclick');
    __fromScript=true;
    var temp = j$("[name=click_me]:first");
    //get the function name from the onclick attribute.
    var evalString = eval(functionName.substring(4,42));
    console.log('the eval string is '+ evalString);
    //From the function name get the function as a string and extract the eval content from it.
    var evalValue = evalString.toString().split('Util.stripCustomFunctionFromObjectPrototype(Array);eval(\'')[1].split('\') } catch (e) { alert(\'A problem with the OnClick JavaScript for this button or link was encountered:')[0];
    console.log('the eval content are '+ evalValue);
    //the eval content are if(__fromScript){\r\n accountId = 10;\r\n}\r\n\r\n \r\n \r\n 
    eval(evalValue);// at this line I get a error stating 'Uncaught SyntaxError: Unexpected token ILLEGAL'
所以我决定用引号来封装eval字符串,这根本不影响结果。eval运行时没有任何问题,但是
accountId
变量是
未定义的

eval('\''+evalValue+'\'');
console.log('the account Id is '+ accountId);
//the account Id is undefined.
最后我硬编码了
evalValue
,然后代码按预期工作

evalValue = 'if(__fromScript){\r\n accountId = 10;\r\n}\r\n\r\n ';
eval(evalValue);
console.log('the account Id is '+ accountId);
//the account Id is 10

eval中的动态引用未正确计算的任何原因。

如果只需要accountID,请执行以下操作

var functionString = j$("[name=click_me]").prop("onclick");
var accountId = parseInt(functionString.split("accountId = ")[1]);

否则,首先删除所有\r\n

我需要从该脚本中获取其他变量。我不能简单地拆分eval脚本来实现这一点。实际上,我只是想了解,如果动态获取字符串,为什么
eval()
的行为会有所不同