div*_*nci 154 javascript
如何执行一些字符串的JavaScript?
function ExecuteJavascriptString()
{
var s = "alert('hello')";
// how do I get a browser to alert('hello')?
}
Run Code Online (Sandbox Code Playgroud)
Len*_*ann 207
小智 117
您可以使用函数执行它.例:
var theInstructions = "alert('Hello World'); var x = 100";
var F=new Function (theInstructions);
return(F());
Run Code Online (Sandbox Code Playgroud)
coo*_*ird 60
该eval函数将评估传递给它的字符串.
但使用eval可能很危险,所以要谨慎使用.
编辑: annakata有一个好点 - 不仅eval 危险,而且很 慢.这是因为必须在现场解析要评估的代码,因此需要一些计算资源.
cgp*_*cgp 20
使用eval().
W3学校游览eval.网站有一些可用的eval示例.Mozilla文档详细介绍了这一点.
您可能会收到很多关于安全使用它的警告.不允许用户将任何内容注入eval(),因为它是一个巨大的安全问题.
您还需要知道eval()具有不同的范围.
小智 15
试试这个:
var script = "<script type=\"text/javascript\"> content </script>";
//using jquery next
$('body').append(script);//incorporates and executes inmediatelly
Run Code Online (Sandbox Code Playgroud)
我个人没有测试它,但似乎工作.
Eri*_*thi 14
对于使用 node 并且关心eval()nodejs 提供的上下文影响的用户vm。它创建了一个 V8 虚拟机,可以在单独的上下文中沙箱化您的代码的执行。
更进一步的是vm2,这会加强vm允许 vm 运行不受信任的代码。
https://nodejs.org/api/vm.html - 官方 nodejs/vm
https://github.com/patriksimek/vm2 - 扩展的 vm2
const vm = require('vm');
const x = 1;
const sandbox = { x: 2 };
vm.createContext(sandbox); // Contextify the sandbox.
const code = 'x += 40; var y = 17;';
// `x` and `y` are global variables in the sandboxed environment.
// Initially, x has the value 2 because that is the value of sandbox.x.
vm.runInContext(code, sandbox);
console.log(sandbox.x); // 42
console.log(sandbox.y); // 17
console.log(x); // 1; y is not defined.
Run Code Online (Sandbox Code Playgroud)
Ant*_*ber 10
有点像@Hossein Hajizadeh alerady所说的,尽管更详细:
有另一种选择eval().
该函数setTimeout()被设计为在一毫秒的间隔后执行某些操作,并且要执行的代码恰好被格式化为字符串.
它会像这样工作:
ExecuteJavascriptString(); //Just for running it
function ExecuteJavascriptString()
{
var s = "alert('hello')";
setTimeout(s, 1);
}Run Code Online (Sandbox Code Playgroud)
1 意味着它将在执行字符串之前等待1毫秒.
它可能不是最正确的方法,但它确实有效.
使用eval如下.Eval应该谨慎使用,关于" eval is evil " 的简单搜索应该引出一些指示.
function ExecuteJavascriptString()
{
var s = "alert('hello')";
eval(s);
}
Run Code Online (Sandbox Code Playgroud)
New Function 和apply()也可以一起使用
var a=new Function('alert(1);')
a.apply(null)
Run Code Online (Sandbox Code Playgroud)
如果要在特定时间后执行特定命令(即字符串) - cmd =您的代码 - InterVal =延迟运行
function ExecStr(cmd, InterVal) {
try {
setTimeout(function () {
var F = new Function(cmd);
return (F());
}, InterVal);
} catch (e) { }
}
//sample
ExecStr("alert(20)",500);
Run Code Online (Sandbox Code Playgroud)
在许多复杂且模糊的脚本上进行了检查:
var js = "alert('Hello, World!');" // put your JS code here
var oScript = document.createElement("script");
var oScriptText = document.createTextNode(js);
oScript.appendChild(oScriptText);
document.body.appendChild(oScript);
Run Code Online (Sandbox Code Playgroud)
我正在回答类似的问题,并得到了另一个想法如何在不使用的情况下实现这一目标eval():
const source = "alert('test')";
const el = document.createElement("script");
el.src = URL.createObjectURL(new Blob([source], { type: 'text/javascript' }));
document.head.appendChild(el);
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,您基本上创建了包含脚本的 Blob,以便创建对象 URL(浏览器内存中文件或 Blob 对象的表示)。由于标签上有src属性<script>,因此脚本的执行方式与从任何其他 URL 加载的方式相同。
小智 5
new Function('alert("Hello")')();
Run Code Online (Sandbox Code Playgroud)
我认为这是最好的方法。
| 归档时间: |
|
| 查看次数: |
177587 次 |
| 最近记录: |