Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preload second page while viewing the current page

Tags:

jquery

ajax

Let's say my website has 5 pages of content. Is it possible to preload the next two pages while visitor is viewing the first page. So that when they click the link to 2nd or 3rd page, it will appear immediately.

like image 893
Clement Ckh Avatar asked May 28 '26 23:05

Clement Ckh


1 Answers

Sure, you can have some invisble containers like this:

HTML

<div id="page">
  <a href="page2.html" id="page2-link">Go second page</a>
</div>
<div id="page2">
</div>

CSS

#page2 { display:none }

Javascript

Then when at the javascript preload the second page after it has been loaded and put it on the invisble container:

$().ready(function(){
  $('#page2').load('page2.html #page');
});

And when the link is clicked just show the hidden container with selected page and remove the other, or just hide it to not load again when user wants to return or something.

$().ready(function(){
  $('#page2-link').on('click',function(){
       $('#page').html( $('#page').html() ); //this will replace html content
  });
});
like image 166
Skatox Avatar answered May 31 '26 12:05

Skatox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!