[英]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.