通过jquery ajax调用评估脚本

时间:2022-07-02 01:27:05

Can anyone tell me why the following function might not be working. The get call should return a mix of html and <script>. I'm trying to find the script element and then evaluate what is inside. But it doesn't seem to be working.

谁能告诉我为什么下面的函数可能不工作。get调用应该返回html和

Any ideas?

什么好主意吗?

function showRecaptcha()
{
alert("test1");
var file = "recapatchatest.php";
$("div#commentWindow").get("../commentfiles/" + file, function(data)
{
  var script = data.getElementsByTagName("script");
  $("div#commentWindow").html(eval(script));
});
}

5 个解决方案

#1


2  

You are not using the correct .get() method. You have to differenciate these two:

您没有使用正确的.get()方法。你必须区分这两个

  • .get() (the one you're using) - retrieves a DOM element at the specified index from the matched elements in the jquery object.

    get()——从jquery对象中的匹配元素中检索指定索引处的DOM元素。

  • jQuery.get() ($.get()) - load data from the server using a HTTP GET

    GET () ($.get()) -使用HTTP GET从服务器加载数据

So you should obviously be using the second one.

显然你应该用第二个。

Be aware that the content of data will not be a DOM element on which you could call getElementByTagName, it is just a string of HTML data.

请注意,数据的内容将不是一个您可以调用getElementByTagName的DOM元素,它只是一个HTML数据的字符串。


If you are trying to load html into an existing element, you should consider using .load() which does exactly what you're after:

如果您正在尝试将html加载到一个现有的元素中,那么您应该考虑使用.load(),它的作用与您想要的完全相同:

.load( url [, data] [, complete(responseText, textStatus, XMLHttpRequest)] ) Returns: jQuery

.load(url [, data]] [, complete(responseText, textStatus, XMLHttpRequest)])返回:jQuery

Description: Load data from the server and place the returned HTML into the matched element.

说明:从服务器加载数据并将返回的HTML放置到匹配的元素中。

#2


1  

I think you should try $.get instead of $("div#commentWindow").get

我想你应该试试$。得到,而不是$(" div # commentWindow "). get

#3


1  

Loading an HTML page via AJAX, parsing out the <script> tags, and evaling them sounds like a bad idea.

通过AJAX加载一个HTML页面,解析

When appended to the DOM, <script> tags will automatically run. So, just append data to the DOM, and your scripts will run.

当附加到DOM时,

Or, put the scripts in their own files, and load 'em with $.getScript.

或者,将脚本放在它们自己的文件中,并使用$. getscript加载它们。

Also, $("div#commentWindow").get should be changed to just $.get.

此外,$(" div # commentWindow”)。get应该改为$.get。

$.get("../commentfiles/" + file, function(data)

#4


0  

Try replacing

尝试更换

$("div#commentWindow").get("../commentfiles/" + file, function(data)
{
  var script = data.getElementsByTagName("script");
  $("div#commentWindow").html(eval(script));
});

with

$.get("../commentfiles/" + file, function(data){
  var script = data.getElementsByTagName("script");
  $("div#commentWindow").html(eval(script));
});

Your original code wasn't actually making an ajax request.

您的原始代码实际上并没有发出ajax请求。

Additionally, to get the scripts, try this as the success handler:

另外,要获得脚本,请尝试将其作为成功处理程序:

var $content = $("#commentWindow");
$.get("../commentfiles/" + file, function(data){
    var html = data;
    html = html.replace(/<script/ig, "<div class='script'").replace(/<\/script/ig, "</div>");
    var html = $(html);
    var scripts = html.filter("div.script").add($(html).find("div.script")).detach();
    $content.html(html);
    scripts.each(function(){
        var $this = $(this), s = document.createElement("script");
        if ($this.attr('src') != "") {
            s.src = $this.attr('src');
        } else {
            s.nodeValue = $this.text();
        }
        $content[0].appendChild(s);
    });
});

The above code has been updated based on comments and now correctly handles both script includes and inline script tags.

上面的代码已经根据注释进行了更新,现在正确地处理了脚本包含和内联脚本标记。

#5


0  

jQuery's get method passes a string to the callback function. data.getElementsByTagName doesn't work because getElementsByTagName is not a valid method for an instance of the String object.

jQuery的get方法将字符串传递给回调函数。数据。getElementsByTagName不起作用,因为getElementsByTagName不是字符串对象实例的有效方法。

I'm not sure why you use the eval method at all. That method is for JavaScript code, and even ignoring that your assignment to the script variable is flawed your intent was to invoke eval on an array of HTML SCRIPT elements.

我不确定为什么要使用eval方法。该方法用于JavaScript代码,甚至忽略对脚本变量的赋值是有缺陷的,您的意图是在一个HTML脚本元素数组上调用eval。

The final issue is that jQuery's html method ignores SCRIPT tags in any string of HTML markup that you pass to it.

最后一个问题是jQuery的html方法忽略了传递给它的任何html标记字符串中的脚本标记。

If you want to add SCRIPT tags to the DOM you're going to need to use plain ol' vanilla JavaScript and bypass jQuery altogether.

如果您想向DOM添加脚本标记,您需要使用普通的纯JavaScript并完全绕过jQuery。

#1


2  

You are not using the correct .get() method. You have to differenciate these two:

您没有使用正确的.get()方法。你必须区分这两个

  • .get() (the one you're using) - retrieves a DOM element at the specified index from the matched elements in the jquery object.

    get()——从jquery对象中的匹配元素中检索指定索引处的DOM元素。

  • jQuery.get() ($.get()) - load data from the server using a HTTP GET

    GET () ($.get()) -使用HTTP GET从服务器加载数据

So you should obviously be using the second one.

显然你应该用第二个。

Be aware that the content of data will not be a DOM element on which you could call getElementByTagName, it is just a string of HTML data.

请注意,数据的内容将不是一个您可以调用getElementByTagName的DOM元素,它只是一个HTML数据的字符串。


If you are trying to load html into an existing element, you should consider using .load() which does exactly what you're after:

如果您正在尝试将html加载到一个现有的元素中,那么您应该考虑使用.load(),它的作用与您想要的完全相同:

.load( url [, data] [, complete(responseText, textStatus, XMLHttpRequest)] ) Returns: jQuery

.load(url [, data]] [, complete(responseText, textStatus, XMLHttpRequest)])返回:jQuery

Description: Load data from the server and place the returned HTML into the matched element.

说明:从服务器加载数据并将返回的HTML放置到匹配的元素中。

#2


1  

I think you should try $.get instead of $("div#commentWindow").get

我想你应该试试$。得到,而不是$(" div # commentWindow "). get

#3


1  

Loading an HTML page via AJAX, parsing out the <script> tags, and evaling them sounds like a bad idea.

通过AJAX加载一个HTML页面,解析

When appended to the DOM, <script> tags will automatically run. So, just append data to the DOM, and your scripts will run.

当附加到DOM时,

Or, put the scripts in their own files, and load 'em with $.getScript.

或者,将脚本放在它们自己的文件中,并使用$. getscript加载它们。

Also, $("div#commentWindow").get should be changed to just $.get.

此外,$(" div # commentWindow”)。get应该改为$.get。

$.get("../commentfiles/" + file, function(data)

#4


0  

Try replacing

尝试更换

$("div#commentWindow").get("../commentfiles/" + file, function(data)
{
  var script = data.getElementsByTagName("script");
  $("div#commentWindow").html(eval(script));
});

with

$.get("../commentfiles/" + file, function(data){
  var script = data.getElementsByTagName("script");
  $("div#commentWindow").html(eval(script));
});

Your original code wasn't actually making an ajax request.

您的原始代码实际上并没有发出ajax请求。

Additionally, to get the scripts, try this as the success handler:

另外,要获得脚本,请尝试将其作为成功处理程序:

var $content = $("#commentWindow");
$.get("../commentfiles/" + file, function(data){
    var html = data;
    html = html.replace(/<script/ig, "<div class='script'").replace(/<\/script/ig, "</div>");
    var html = $(html);
    var scripts = html.filter("div.script").add($(html).find("div.script")).detach();
    $content.html(html);
    scripts.each(function(){
        var $this = $(this), s = document.createElement("script");
        if ($this.attr('src') != "") {
            s.src = $this.attr('src');
        } else {
            s.nodeValue = $this.text();
        }
        $content[0].appendChild(s);
    });
});

The above code has been updated based on comments and now correctly handles both script includes and inline script tags.

上面的代码已经根据注释进行了更新,现在正确地处理了脚本包含和内联脚本标记。

#5


0  

jQuery's get method passes a string to the callback function. data.getElementsByTagName doesn't work because getElementsByTagName is not a valid method for an instance of the String object.

jQuery的get方法将字符串传递给回调函数。数据。getElementsByTagName不起作用,因为getElementsByTagName不是字符串对象实例的有效方法。

I'm not sure why you use the eval method at all. That method is for JavaScript code, and even ignoring that your assignment to the script variable is flawed your intent was to invoke eval on an array of HTML SCRIPT elements.

我不确定为什么要使用eval方法。该方法用于JavaScript代码,甚至忽略对脚本变量的赋值是有缺陷的,您的意图是在一个HTML脚本元素数组上调用eval。

The final issue is that jQuery's html method ignores SCRIPT tags in any string of HTML markup that you pass to it.

最后一个问题是jQuery的html方法忽略了传递给它的任何html标记字符串中的脚本标记。

If you want to add SCRIPT tags to the DOM you're going to need to use plain ol' vanilla JavaScript and bypass jQuery altogether.

如果您想向DOM添加脚本标记,您需要使用普通的纯JavaScript并完全绕过jQuery。