在AJAX加载内容之后加载脚本

在AJAX加载内容之后加载脚本

本文介绍了在AJAX加载内容之后加载脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQueryYQL来获取网站的内容并在页面上显示某个元素,在本例中为table元素.我还使用 jQuery QuickSearch 插件设置了搜索功能.

I'm using jQuery and YQL to get the content of a website and display a certain element on the page, in this case, the table element. I also have a search function set up using the jQuery QuickSearch plugin.

这很好用,现在解决了问题...

似乎在AJAX内容获取程序获取内容之前,正在加载搜索脚本.我认为脚本随后会缓存数据,以便于搜索.由于它是在内容存在之后加载的,因此它不会搜索任何内容,也无法正常工作.有什么方法可以加载AJAX内容后之后加载搜索脚本?

It seems that the search script is loading before the AJAX content getter gets the content. I think the script then caches the data, so that it's easier to search. Since it's loading after the content is there though, it doesn't search anything and it doesn't work. Is there any way to load the search script AFTER the AJAX content is loaded?

我已经尝试通过jQ中的ready函数调用它:

I already attempted calling it via the ready function in jQ:

$(document).ready(function() {

但这仍然在加载内容之前加载.我的AJAX脚本如下:

But that still loads before the content is loaded. My AJAX script is below:

<script type="text/javascript">
    $(document).ready(function () {
        var container = $('#content');
        function doAjax(url) {
            if (url.match('^http')) {
                $.getJSON("http://query.yahooapis.com/v1/public/yql?"+
                    "q=select%20*%20from%20html%20where%20url%3D%22"+
                    encodeURIComponent(url)+
                    "%22&format=xml'&callback=?",
                function (data) {
                    if (data.results[0]) {
                        var fullResponse = $(filterData(data.results[0])),
                            justTable = fullResponse.find("table");
                        container.append(justTable);
                    } else {
                        var errormsg = '<p>Error: could not load the page.</p>';
                        container.html(errormsg);
                    }
                });
            } else {
                $('#content').load(url);
            }
        }
        function filterData(data) {
            data = data.replace(/<?\/body[^>]*>/g, '');
            data = data.replace(/[\r|\n]+/g, '');
            data = data.replace(/<--[\S\s]*?-->/g, '');
            data = data.replace(/<noscript[^>]*>[\S\s]*?<\/noscript>/g, '');
            data = data.replace(/<script[^>]*>[\S\s]*?<\/script>/g, '');
            data = data.replace(/<script.*\/>/, '');
            data = data.replace(/<img[^>]*>/g, '');
            return data;
        }
        doAjax('url_goes_here');
});
</script>

推荐答案

不确定我是否真的得到了这个问题,但是要在使用Ajax加载内容之后加载脚本,您可以执行以下操作:

Not sure I really get the question, but to load a script after the content is loaded with Ajax you could do something like this:

$.ajax({
  url: url,
  data: data,
  success: function() {
     //success loading content
  },
  error: function() {
    //error loading content
  },
  complete: function() {
     $.getScript("/scripts/mysearchscript.js", function() {
          alert('loaded script and content');
     });
  }
});

这篇关于在AJAX加载内容之后加载脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 00:50