执行用包含脚本标记的XMLHttpRequest编写的Javascript? [英] Executing Javascript written with XMLHttpRequest that contains script tags?

查看:77
本文介绍了执行用包含脚本标记的XMLHttpRequest编写的Javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过Javascript请求, XMLHttpRequest 使用一些需要添加到请求页面页面的其他Javascript进行响应。

Through a Javascript request, XMLHttpRequest responds with some additional Javascript that needs to be added to the page the requesting page.

使用eval(),如果响应类似于:

Using eval(), if the response is something like:

alert('This is the js response');

...然后这很好用。

... then this works just fine.

但是,返回的文字可能如下所示:

However, the text returned could look something like this:

<script language="javascript">var checkVar='checkVar: value 1';</script>

但很有可能:

<script src="http://www.somesite.com/setCheckVarValue.js"></script> 

...需要在页面上加载额外的JS。

... where additional JS needs to be loaded on the page.

我确保 XMLHttpRequest 同步,因为我想引用checkVar吧在此之后。

I have ensured that the XMLHttpRequest is synchronous, as I want to reference checkVar right after this.

所以:

<script type="text/javascript" src="http://www.mysite.com/addJSToPage.js" />
// at this point, since this is a synchronous call, page processing waits
// until the response is received that needs to include the additional JS
// to load; this, for testing sets the value of checkVar

<script type="text/javascript" >
    alert(checkVar);
</script>

警告信息应为checkVar:value 1。

The alert message should read "checkVar: value 1".

由于其他原因,这不仅仅是在 addJSToPaged.js 中设置 var checkVar 这么简单,所以我不是寻找那种推荐。

For other reasons, this is not just as simple as setting var checkVar in addJSToPaged.js, so I'm not looking for that kind of recommendation.

我正在使用 alert(checkVar)作为测试,以确保在响应中通过JS设置了值。

I'm using alert(checkVar) simply as a test to ensure that a value has been set through JS in the response.

我想我可以删除开始和结束脚本标签并保持 eval()的方式。但是,我想知道是否有任何解决方案支持我正在寻找的东西?

I suppose that I could strip out the beginning and ending script tags and keep the eval() way of doing it. However, I would like to know if there are any solutions that support what I'm looking for?

谢谢。

更新

根据Prashanth的建议,在addJSToPage.js中我添加了:

Following Prashanth's suggestion, in addJSToPage.js I added:

var dynamicElement = document.createElement('div');

然后在XMLHttpRequest的响应中,我做了:

Then in the response from the XMLHttpRequest, I did:

dynamicElement.appendChild = xmlhttp.responseText;

仍未看到checkVar的值。

Still not seeing the value of checkVar.

推荐答案

重新启动 - 需要能够在加载后/加载期间将某些内容动态加载到页面上并让它执行。通过执行,我不仅仅意味着更改某些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.

这篇关于执行用包含脚本标记的XMLHttpRequest编写的Javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆