views:

837

answers:

3

i have two links

<a href="page1.html">page 1</a>
<a href="page2.html">page 2</a>

<div id="content">&nbsp;</div>

1. I use JQuery to load them into a Div when the links are clicked.

$('#content').load('page1.html');

2. When the second link is clicked I empty the div

$('#content').html('');

3. and load the second file in;

$('#content').load('page2.html');

The problem, page1.html has alot of images in it, even after the second link is clicked and the #content div is emptied, the browser keeps downloading the images from page1.html and this slows down the request to page2.html significantly.

How can i stop the content from the first load() from making requests to the browser and speed up the page response?

Thank you infinity!

+1  A: 

I suspect that the problem isn't with the load() method, but rather the requests that are generated when the DOM is updated as a result of the load request completing. Once the requests for the images have been initiated I don't think there is any way short of reloading the page that you can stop those requests from being handled. You could, potentially, check to see if the request has been handled yet when the second link is clicked and reload the page rather than use AJAX if the request is still in progress. I'm not sure this is really worth it though.

tvanfosson
Thank you, that made me realize the nature of this problem much more clearly :)
Mohammad
I'm going to look in the lazy load jQuery plugin, it seems like it has an image loading control system of sorts :)http://www.appelsiini.net/projects/lazyload
Mohammad
I've updated the question, since the initial question was based on wrong logic.
Mohammad
+1  A: 

I don't think you'll be able to use load. I think you'll need to use the ajax function.

From the docs:

$.ajax() returns the XMLHttpRequest that it creates. In most cases you won't need that object to manipulate directly, but it is available if you need to abort the request manually.

It's more code, but shouldn't be that bad. You will probably need to tweak this code (it's untested), but I think the concept will work.

var xhr;

$("a").click(function() { 
  if(xhr !== undefined) { xhr.abort(); } 
  xhr = $.ajax({
    url: this.href,
    success: function(data) {
       $("#content").html(data);
    }
  });
});
Andy Gaskell
thank you!i guess i could then usefunction abort() {xhr.abort();}to cancel the previous ajax request on the next onclick event, yes?
Mohammad
That's my thinking - what I'm unsure of is the value of xhr after a request has been completed. actually I'd guess it's not undefined. I'm going to add xhr = undefined to the code - you can take it out if it's unnecessary.
Andy Gaskell
you're right, it's not undefined. Could I ask the purpose of your last line "xhr = undefined;"shouldn't we leave xhr as is, so that on the next click, your xhr.abort(); will work?
Mohammad
I've used your code, without the last line "xhr = undefined" and it does speed up the page response, thank you so much! i'm still curious to the purpose of the line btw :)
Mohammad
Actually most of the xhr code can probably go. You can take out the if statement and the assignment to undefined.
Andy Gaskell
yes that is true :)
Mohammad
Mohammad
A: 

Would it be possible to un-AJAX-ify this page? Most browsers I know of stop loading a page (page1) when they leave it. The whole point of AJAX is to make responsive web applications. In this case AJAX seems to be counterproductive toward that goal. My guess is that re-loading all of the content in the page besides the content in the #content div would be faster than finishing loading page1.

Imagist
That is so true, my implementation of Ajax is a very 'counterproductive'. I think now, i need a new implementation. One that will introduce new images from page1.html to #content as they load to the browsers cache, and have a way of canceling the process prematurely if page2 is requested.That way it will not only be faster, yet save bandwidth and make the page load more enjoyably.I'll be looking at the lazyload plugin for JQuery for new ideas.
Mohammad
I think you're missing the point of my post: my suggestion was to not use JQuery to load data into the page (this is the basic "AJAX" technique), but instead to load a completely new page in the more traditional manner. This allows the browser, rather than your script, to handle cancelling/not sending the requests, and based on clicking some high-image-density pages and then clicking a link before they have a chance to load, it will work (in Firefox, Safari, IE8, and Chrome, all on Windows).
Imagist
I understand that, though i'm trying to create a client side include that would replace the need of a php include. I know this will probably be wrong on SEO level. Though I'm very determined to make it more responsive.
Mohammad