简体   繁体   English

如何使嵌入式JavaScript在.innerHTML编写中运行?

[英]How do I get embedded JavaScript to run in a .innerHTML write?

I previously had this problem with an external file not running. 我以前在外部文件未运行时遇到此问题。

I fixed this by loading the file dynamically instead...ie creating the element in JavaScript and linking to it via the .src attribute ( did this instead of using a scrip tag in the html ). 我通过动态加载文件来解决此问题,即...在JavaScript中创建元素并通过.src属性链接到该元素(这样做是为了代替在html中使用scrip标记)。 Do I need to do something similar here? 我需要在这里做类似的事情吗?

Here is the embedded JavaScript I need to run that is inserted via .innerHTML of the body. 这是通过正文的.innerHTML插入的我需要运行的嵌入式JavaScript。 Currently it is just "dead code" in my page. 目前,这只是我页面中的“无效代码”。 Note the json I'm passing in as the second variable to the set function. 请注意,我将json作为第二个变量传递给set函数。 This is PHP composed and passed to the client via ajax POST. 这是PHP组成的,并通过ajax POST传递给客户端。

<script type='text/javascript'>new Arc.Shared().set( 'tweet_data', [{"id":"1","0":"1","picture":"0","1":"0","name":"Test Account","2":"Test Account","tweet":"Hi","3":"Hi","time":"1338559048","4":"1338559048"},{"id":"1","0":"1","picture":"0","1":"0","name":"Test Account","2":"Test Account","tweet":"hi","3":"hi","time":"1338558809","4":"1338558809"},{"id":"1","0":"1","picture":"0","1":"0","name":"Test Account","2":"Test Account","tweet":"<a class=\"bookmark_tweet\" target=\"_blank\" href=\"http:\/\/ebay.com\">ebay <\/a>","3":"<a class=\"bookmark_tweet\" target=\"_blank\" href=\"http:\/\/ebay.com\">ebay <\/a>","time":"1338504456","4":"1338504456"},{"id":"1","0":"1","picture":"0","1":"0","name":"Test Account","2":"Test Account","tweet":"foo","3":"foo","time":"1338504225","4":"1338504225"},{"id":"1","0":"1","picture":"0","1":"0","name":"Test Account","2":"Test Account","tweet":"foo","3":"foo","time":"1338504222","4":"1338504222"},{"id":"1","0":"1","picture":"0","1":"0","name":"Test Account","2":"Test Account","tweet":"foo","3":"foo","time":"1338504220","4":"1338504220"},{"id":"1","0":"1","picture":"0","1":"0","name":"Test Account","2":"Test Account","tweet":"foo","3":"foo","time":"1338504217","4":"1338504217"}] );</script>

Notes: 笔记:

Alternative to Eval Snippet: 评估代码段的替代方法:

var myCode = 'alert("Howdy?");';
 var myFucn = new Function(myCode);
 myFucn();

If you want to run AJAX injected JS, you should call "eval()" on it (for security reasons). 如果要运行AJAX注入的JS,则应出于安全原因在其上调用“ eval()”。 Another possibility is to load it via a <script src='[url to your JS]'></script> tag. 另一种可能性是通过<script src='[url to your JS]'></script>标记加载它。

Another way per Why doesn't embedded JavaScript execute when inserted in response to an ajax call? 为什么在插入Ajax调用时嵌入的JavaScript不执行?

Here is the append child method...that take ajax responseText and appends it into the DOM. 这是append child方法...采用ajax responseText并将其附加到DOM中。

function onSuccess() {
    src = document.createElement('script');
    src.innerHTML = this.responseText;
    document.body.appendChild(src);
}

Third way is to have an external element trigger it per here: 第三种方法是让外部元素按此处触发:

Executing <script> elements inserted with .innerHTML 执行插入.innerHTML的<script>元素

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

相关问题 如何在JavaScript中获取innerHTML(总字符数)的长度? - How do i get the length of innerHTML (total characters) in JavaScript? 如何使用 javascript 获取所选 Selenium WebElement 的 innerHTML? - How do I get the innerHTML of the selected Selenium WebElement using javascript? 如果 display.innerHTML 为空,如何编写算术运算符不运行的 IF ELSE 语句? - How do I write an IF ELSE statement that my arithmetic operators don't run if the display.innerHTML is empty? 如何在 InnerHTML javascript 中添加换行符 - How do I add linebreaks in a InnerHTML javascript 如果我将某些内容写到innerHTML中,如何获得准确的副本? - If I write something into innerHTML, how do I get exact copy back? 在 Puppeteer 中,如何获取选择器的 innerHTML? - In Puppeteer, how do I get the innerHTML of a selector? 如何获得iframe的innerHTML? - How do I get the innerHTML of an iframe? 如何运行以嵌入形式插入.innerHTML的javascript? - How does one run javascript that has been inserted into a .innerHTML in embedded form? 如何在没有Javascript生成内容的情况下获取原始的innerHTML源代码? - How do I get the original innerHTML source without the Javascript generated contents? 当内部HTML由Javascript函数生成时,如何获取页面一部分的innerHTML? - How do I get the innerHTML of a part of my page when that inner HTML is generated by a Javascript function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM