简体   繁体   English

jQuery / Javascript:AJAX请求引入的HTML不包括<script> elements

[英]jQuery/Javascript: HTML pulled in by AJAX request does not include <script> elements

I have a dynamic page which uses an AJAX request kicked off by jQuery to pull in HTML elements from the server and insert them into the DOM. 我有一个动态页面,该页面使用jQuery启动的AJAX请求从服务器中提取HTML元素并将其插入DOM。

The problem is that when I have elements within the response, they are stripped out. 问题是,当我在响应中包含元素时,它们就会被删除。

For instance, if I request the following from the server: 例如,如果我从服务器请求以下内容:

<!-- content.html -->
<div>
    There is some content here!
    <script>
        manipulateContent();
    </script>
</div>

What actually gets injected into my dynamic page is the following: 实际注入到我的动态页面的内容如下:

<!-- content.html -->
<div>
    There is some content here!
</div>

I have tested in Chrome, Firefox, and Safari with identical results. 我已经在Chrome,Firefox和Safari中测试了相同的结果。

The relevant Javascript which creates the AJAX request is here: 创建AJAX请求的相关Javascript在这里:

function loadContent(url){
    var a = document.createElement('a');
    a.href = url;
    if (a.search == ""){ 
        url = url + "?trim=true";
    } else {
        url = url + "&trim=true";
    }

    var ch = $('#content-container').height();
    // var wh = $(window).height();

    $("#content").animate({top: '-='+ch+'px'}, 500, function(){
        $.get(url, function(data){
            $("body").scrollTop(0);
            $("#content").html(data);
            $("#content").css({top: ch+'px'});
            $("#content").animate({top: '0px'}, 500);
        });
    });
}

$(document).ready(function(){

    // get the current path and save it for later
    var currentPage = location.pathname+location.search;

    $(".content-link").live("click", function(){
        // using the HTML5 history API, add the requested path
        // to the browser history, then load the new content
        history.pushState({ path: this.path }, '', this.href);
            // because the page is not reloaded, $(document).ready()
            // is not called, so the currentPath must be updated manually
                currentPage = this.href;
        loadContent(currentPage);

        return false;

    });

    window.addEventListener('popstate', function() {
        // compare the current path to the one being loaded
        // if they are different, then load the content
        // else, nothing happens
        if (currentPage != location.pathname+location.search){
            // because the page is not reloaded, $(document).ready()
            // is not called, so the currentPath must be updated manually
            currentPage = location.pathname+location.search;
            loadContent(currentPage);
        }
    });

});

How can I tell jQuery to include the tags in the response? 如何告诉jQuery在响应中包含标签? I've tried browsing through the jQuery docs without much luck, or even mention of the fact that the tags are stripped out. 我试过浏览jQuery文档时运气不佳,甚至没有提到标签被剥离的事实。 Perhaps I'm just not looking in the right places. 也许我只是在找对地方。

You need to use load , since the whole purpose here is to load hml content to a element. 您需要使用load ,因为这里的全部目的是将hml内容加载到元素。

function loadContent(url) {
    var a = document.createElement('a');
    a.href = url;
    if (a.search == "") {
        url = url + "?trim=true";
    } else {
        url = url + "&trim=true";
    }

    var ch = $('#content-container').height();
    // var wh = $(window).height();

    $("#content").animate({
        top : '-=' + ch + 'px'
    }, 500, function() {
        $("#content").load(url, function() {
            $("body").scrollTop(0);
            $("#content").css({
                top : ch + 'px'
            });
            $("#content").animate({
                top : '0px'
            }, 500);
        });
    });
}

According to jQuery documentation ( http://api.jquery.com/jQuery.ajax/ ) if dataType option is html : 根据jQuery文档( http://api.jquery.com/jQuery.ajax/ ),如果dataType选项为html

Returns HTML as plain text; 以纯文本形式返回HTML; included script tags are evaluated when inserted in the DOM. 插入到DOM中时,将评估包含的脚本标签。

By default this option is set to Intelligent Guess , so you may want to check the type of response from the server. 默认情况下,此选项设置为Intelligent Guess ,因此您可能要检查服务器的响应类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM