简体   繁体   English

执行以字符串形式存储的 JavaScript 代码<Script> tag

[英]Execute JavaScript code stored as a string with <Script> tag

This post is more related with the question asked here.这篇文章与这里提出的问题更相关。 Execute JavaScript code stored as a string 执行以字符串形式存储的 JavaScript 代码

but my problem seems little different here.但我的问题在这里似乎没什么不同。

When I am trying to execute code like this, it errors out unexpected token '<'当我尝试执行这样的代码时,它会错误地输出unexpected token '<'

setTimeout('<script>console.log("some string")</script>', 1);

Facing same issue with new Function( '<script>console.log("some string")</script>' )面临同样的问题new Function( '<script>console.log("some string")</script>' )

    let incomingString='<script>console.log("some string")</script>';
    let f = new Function(incomingString.replace("<script>","").replace("</script>",""));
    f();

Thiw code on Node outputs: some string but it can't run on snippet节点输出上的 Thiw 代码:一些字符串,但它不能在代码片段上运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM