Skip to content Skip to sidebar Skip to footer

Preload Webpage With JQuery

I found the below script here but wanted to know if it is posible to preload multiple pages with the same script. The reason is I want to preload some forms that will be hold in f

Solution 1:

This script loads the page "index2.html" and puts its content into the div#index2content when the DOM is ready.

So, if you want to load multiple pages, you can do the following:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
  jQuery().ready(function () {
    $.get('index2.html', function(data) {
     jQuery("#index2content").html(data);
    });

    $.get('index3.html', function(data) {
     jQuery("#index3content").html(data);
    });

    $.get('index4.html', function(data) {
     jQuery("#index4content").html(data);
    });
  });
</script>

<div id="index2content"></div>
<div id="index3content"></div>
<div id="index4content"></div>

Solution 2:

Three options:

a) Setup an API that would serve the whole html, then just get the data using $.AJAX, witch would be a bad practice because of overload

b)

<script>
     var html = '<?php echo $html_file;?>'; 
     jQuery("#index2content").html(html);
</script>

c)

<iframe id="data" style="display:none" src="your document">
</iframe>

<script>
$("#data").load(function(){
    $("#index2content").html($("#data").contents().find("body").html());
    $("#data").remove();/detach the iframe
});
</script>

Post a Comment for "Preload Webpage With JQuery"