代码没有在javascript的eval中执行
Code not executing in eval in javascript
我知道'eval '是邪恶的',但在salesforce的标准功能是使用eval在点击按钮时执行脚本,这个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
变量是undefined
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() 是检查程序执行的好方法吗?
- 推荐在JavaScript中执行存储为字符串的函数,而不是使用eval
- 当eval只执行服务器端数据时,在javascript中使用eval是否安全
- eval和setTimeout之间的差异执行字符串代码
- eval() 不执行外部 (src=..) 脚本
- Java 脚本 eval() 函数属于哪个对象?此外,在调用 eval() 函数时传递执行上下文
- MongoDB “eval” 的执行顺序
- eval() 的执行是否仅在调用它的函数中已知(作用域)
- 有没有一种方法可以在JavaScript中执行类似于window.eval()的jQuery
- 为什么可以从JS ScriptEngine eval(String)执行Java代码
- Javascript Eval/从输入框执行代码
- 在不使用eval()的情况下从字符串执行jquery命令
- Eval在执行带有return语句的函数时总是返回错误
- 我可以在全局执行上下文中运行javascript没有eval
- 在JS中从php文件执行字符串,setTimeout vs eval()
- 在没有Eval的Javascript中执行字符串
- 如何执行eval()代码并获取所有参数
- 使用eval()执行用户提供的代码的问题
- 如何执行在JS函数中作为字符串传递的函数调用,没有eval
- 在沙盒中执行eval时缺少窗口中定义的类