且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

执行用包含脚本标记的XMLHttpRequest编写的Javascript?

更新时间:2023-12-05 09:31:22

重新启动 - 需要能够在加载后/加载期间将某些内容动态加载到页面上并让它执行。通过执行,我不仅仅意味着更改某些div上的文本 - 这很容易。但是如果我们想要动态加载一些新的JS,比如来自某些外部源的警报,并注入它,以及它的脚本标记,或者其他一些HTML代码,那么解决方案是使用以下jQuery调用:

Recapping - the need is present to be able to dynamically load some content onto a page after/during load, and have it execute. By execute, I don't just mean change the text on some div - that's easy. But if we want to load some new JS dynamically, say an alert that comes from some outside source, and inject it, along with it's script tags, and maybe some other HTML code, then the solution is to use the following jQuery call:

jQuery(dynamicResponse).appendTo('body');

dynamicResponse 来自异步 $。ajax({}) XmlHttpRequest 响应。一旦出现,它将被附加到 appendTo()中指定的任何DOM元素并执行。

dynamicResponse comes from an asynchronous $.ajax({}) or XmlHttpRequest response. Once present, it is appended onto whatever DOM element, specified in appendTo() and executed.