Preload Webpage with jQuery

允我心安 提交于 2019-12-02 09:09:40

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>

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!