How to preload all default tabs using jQuery

If I have 4 tabs where the first 2 are loaded using ajax and the last 2 are static, how do I preload ajax 2 tabs by default?

At the moment, only the first tab is automatically loaded, the second - when pressed. I want both of them to be loaded, so when I click the second, the content is already loaded. I tried to trigger the load event on the second tab as follows:

$(document).ready(function () {
    $("#main-tabs").tabs({
        cache: true
    });

    $("#main-tabs").tabs("load", 1);
});

And this loads the second tab, but for some strange reason, the first does not load at all; even when I click on another tab and click on the first, it will not load.

Then I tried something like this:

$(document).ready(function () {
    $("#main-tabs").tabs({
        cache: true
    });

    $("#main-tabs").tabs("load", 0);
    $("#main-tabs").tabs("load", 1);
});

The same result, the second tab is loaded, the first is not.

How do all of them (ajax) load automatically?

+3
1

:

  • jQuery load, AJAX, (, , AJAX, , jQuery , - ).
  • first AJAX, .tabs("load",0), .

, , . load , , , . .

load , . load , . , , . , ..

//the indexes to be loaded.
//In your case it only index 1 (index 0 is loaded automatically)
var indexesToLoad = [1];

var loadNextTab = function() {
    if (indexesToLoad.length == 0) return;
    var index = indexesToLoad.shift();
    $("#main-tabs").tabs("load",index);
};

$("#main-tabs").tabs({
    cache: true,
    load: loadNextTab
});

:

  • , , AJAX .
  • , loadNextTab .
  • ( )

, ?

firebug. firefox. Firebug AJAX , , , , :) . ( - ctrl + shift + j chrome F12 IE.)

Screenshot of Firebug console showing ajax requests

+7

Source: https://habr.com/ru/post/1764101/


All Articles