NGM
NGM

Reputation: 89

Fastest way to load a part of a file via AJAX?

I have a lot books in text files.

I've listed the Index of the book in website.

The theory is: Load the part of book from that Index from a text file that contains whole that book.

INDEX OF THE BOOK(for example we have 3 indexes):

<ul>
   <li><a onClick="return LoadThis('a1')">INDEX-1</a><div id="loader1" ></div></li>
   <li><a onClick="return LoadThis('a2')">INDEX-2</a><div id="loader2" ></div></li>
   <li><a onClick="return LoadThis('a3')">INDEX-3</a><div id="loader3" ></div></li>
</ul>

The JavaScript Code:

var loadUrl = "http://www.website.com/books/the-book.php";

function LoadThis(link_anchor)
{
var loader="#loader"+link_anchor.substring(1);
$(loader).html(ajax_load).load(loadUrl+" #"+link_anchor);

$(loader).addClass('ajax-active');
}

The-Book.PHP file

<div id="a1"><p>Index-1 texts...</p></div>
<div id="a2"><p>Index-2 texts ....</p></div>
<div id="a3"><p>Index-3 texts ....</p></div>

Problem: The loadings are usually not bad! and their loadings working good. but...

if the count of Indexes and the file-size of The-Book.php has grow and big, the Loading is so slower than the small books.

Question: Is my programming method with AJAX LOAD function an standard method?

Is there a better, more standard, and Faster method to do this?

Please Help me.

Upvotes: 0

Views: 142

Answers (1)

Karl-Johan Sj&#246;gren
Karl-Johan Sj&#246;gren

Reputation: 17522

Working with .load() itself isn't a bad practice in any way but in your case it's not really ideal.

When you fetch a page with .load() and specify a selector to identify which part you want to insert you might think that it would only download that part of the file. Since there is no way of jQuery to know where in the file that selector is (or if it is in several places) it will have to download the whole file even if you just want the first (or last) part of it.

So if you have a large book (say a few hundred kilobytes) and no caching in place for your book you will need to download quite a lot of data several times to fill all sections on your page.

If you really want to do partial loading, then you need to create separate files on your harddrive and load the correct file for each part each time, or you could have your php-file filter the result and only return the requested part.

If you want to go with the idea of filtering using php then you could have a look at How do you parse and process HTML/XML in PHP? for more info about parsing html/xml.

Upvotes: 3

Related Questions