运行JavaScript,这是AJAX响应的一部分 [英] run JavaScript that is part of AJAX response

查看:57
本文介绍了运行JavaScript,这是AJAX响应的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




在我的浏览器中,我发出了一个AJAX请求。服务器向我发送一个HTML文档片段

。该片段在一些

脚本标记中包含一些JavaScript。当片段到达

浏览器时如何运行这些脚本?


谢谢,

Peter

解决方案

pe ***** *****@gmail.com 写道:



在我的浏览器中,我发出了一个AJAX请求。服务器向我发送HTML文档的片段。该片段在一些
脚本标记中包含一些JavaScript。当片段到达浏览器时,如何运行这些脚本?



我认为唯一的跨浏览器解决方案是提取脚本和

eval()它,只要你确定脚本的来源。


-

Ian Collins。




Ian Collins写道:

pe ********** @ gmail.com 写道:

在我的浏览器中,我发出了一个AJAX请求。服务器向我发送HTML文档的片段。该片段在一些
脚本标记中包含一些JavaScript。当片段到达浏览器时,如何运行这些脚本?


我认为唯一的跨浏览器解决方案是提取脚本和
eval()它




你好,Ian,


我不反对eval(),但认为可能有一个

官方批准的解决方案,没有使用eval()。


我试图偷偷摸摸并使用innerHTML插入片段。然后使用

getElementsByTagName来提取脚本元素。这没有工作

那么好。我还在试图弄清楚发生了什么。


你是否通常在原始的

脚本标签上使用一些正则表达式匹配来提取响应?是否有一种特别简单的方法可以将每个脚本标记对的内容添加到每个eval()的数组中?


谢谢,

Peter


pe **********@gmail.com 写道:

Ian Collins写道:

pe ***** *****@gmail.com写道:

在我的浏览器中,我发出了一个AJAX请求。服务器向我发送HTML文档的片段。该片段在一些
脚本标记中包含一些JavaScript。当片段到达浏览器时,如何运行这些脚本?



我认为唯一的跨浏览器解决方案是提取脚本和
eval()它



你好Ian,

我不反对eval(),但认为可能有一个官方批准的解决方案没有使用eval()。

我试图偷偷摸摸地使用innerHTML插入片段。然后使用
getElementsByTagName来提取脚本元素。这样做不好。我还在试图弄清楚发生了什么。

您是否经常使用原始响应中的
脚本标记上的正则表达式匹配进行提取?是否有一种特别简单的方法可以将每个脚本标记对的内容添加到每个eval()的数组中?



我更喜欢JSON用于AJAX应用程序。 />

您是否尝试过发回文件名,创建脚本元素,

设置类型(text / javascript)和src(文件名)属性?


-

Ian Collins。


Hi,

In my browser, I make an AJAX request. The server sends me a fragment
of an HTML document. That fragment has some JavaScript inside some
script tags. How do I run these scripts when the fragment arrives at
the browser?

Thanks,
Peter

解决方案

pe**********@gmail.com wrote:

Hi,

In my browser, I make an AJAX request. The server sends me a fragment
of an HTML document. That fragment has some JavaScript inside some
script tags. How do I run these scripts when the fragment arrives at
the browser?


I think the only cross browser solution is to extract the script and
eval() it, so long as you are sure of the source of the script.

--
Ian Collins.



Ian Collins wrote:

pe**********@gmail.com wrote:

In my browser, I make an AJAX request. The server sends me a fragment
of an HTML document. That fragment has some JavaScript inside some
script tags. How do I run these scripts when the fragment arrives at
the browser?


I think the only cross browser solution is to extract the script and
eval() it



Hi Ian,

I''m not opposed to eval() for this but thought there might be an
officially sanctioned solution that didn''t use eval().

I tried to be sneaky and use innerHTML to insert the fragment. Then use
getElementsByTagName to extract the script elements. This didn''t work
so well. I''m still trying to figure out what is going on.

Do you usually extract using some regular expression match on the
script tags in the raw response? Is there an especially easy way to get
the contents of each script tag pair into an array for eval() of each?

Thanks,
Peter


pe**********@gmail.com wrote:

Ian Collins wrote:

pe**********@gmail.com wrote:

In my browser, I make an AJAX request. The server sends me a fragment
of an HTML document. That fragment has some JavaScript inside some
script tags. How do I run these scripts when the fragment arrives at
the browser?



I think the only cross browser solution is to extract the script and
eval() it


Hi Ian,

I''m not opposed to eval() for this but thought there might be an
officially sanctioned solution that didn''t use eval().

I tried to be sneaky and use innerHTML to insert the fragment. Then use
getElementsByTagName to extract the script elements. This didn''t work
so well. I''m still trying to figure out what is going on.

Do you usually extract using some regular expression match on the
script tags in the raw response? Is there an especially easy way to get
the contents of each script tag pair into an array for eval() of each?


I prefer JSON for AJAX applications.

Have you tried sending back a file name, creating a script element,
setting the type (text/javascript) and src (file name) attributes?

--
Ian Collins.


这篇关于运行JavaScript,这是AJAX响应的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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