weixin_33690367 2011-04-30 03:22 采纳率: 0%
浏览 36

jQuery Ajax加载和getScript

I'm trying to set up my site so that content in the main area is loaded via javascript (some pages take a while to render). However, I've run into a bit of a logical problem when using jQuery.load(), jQuery.getScript and jQuery.ajax().

I need to be able to load certain javascript files via getScript so that I can use these functions etc inside the loaded content.

  • index.php - The main file being displayed (also contains #loading)
  • js/script.js - The file containing the load code
  • js/*.js - Several javascript files I need to be able to use inside #maincontent after the load
  • load.php - The file being loaded into #maincontent

script.js:

$(document).ready(function() {
$('#loading').fadeIn('fast');
$('#maincontent').load('load.php', function() {
    $('#loading').fadeOut('fast');
    $('#maincontent').fadeIn('slow');
});

$('#navigation li a').click(function() {
    $('#maincontent').fadeOut('fast');

    $.getScript('js/jquery.contextMenu-1.01.js');
    $.getScript('js/jquery-1.5.1.min.js');
    $.getScript('js/jquery-ui-1.8.12.custom.min.js');
    $.getScript('js/jquery.qtip-2.0.0.min.js');
    $.getScript('js/ajax.js');

    $.ajax({
        method: 'get',
        url: 'load.php',
        data: 'page=' + $(this).attr('rel'),
        beforeSend: function() {
            $('#loading').fadeIn('fast');
        },
        complete: function() {
            $('#loading').fadeOut('fast');
        },
        success: function(html) {
            $('#maincontent').fadeIn('slow');
            $('#maincontent').html(html);
        }
    });
});

$.getScript('js/jquery.contextMenu-1.01.js');
$.getScript('js/jquery-1.5.1.min.js');
$.getScript('js/jquery-ui-1.8.12.custom.min.js');
$.getScript('js/jquery.qtip-2.0.0.min.js');
$.getScript('js/ajax.js');
});

As you can see, I'm trying to load load.php first without any URL parameters and any interaction from the user. Although load.php is loaded correctly every time, the same can't be said for the javascript code. Sometimes it works, other times I have to refresh the page a few times before I get a clean console (no errors).

The errors in the console suggest to me that the js code isn't loaded properly before it is being used inside load.php. This is especially true if load.php takes a while (tested with PHP sleep(5) function).

Let me know if I need to clarify anything :)

  • 写回答

2条回答 默认 最新

  • weixin_33696822 2011-04-30 03:29
    关注

    You shouldn't really load all of those scripts with getScript. You will need some (or all?) of them every time, so why not just include them on the page. In addition, since your'e using jquery to load jquery, I'm going to say you already have it included on the page. So don't try to load it again.

    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试