tags:

views:

198

answers:

3

My jQuery code (using ajax) request's data from a local php script (pgiproxy.php). This script grabs the desired webpage. I am using the following php function for this:

function grabPage($pageURL) {
  $homepage = file_get_contents($pageURL);
  echo $homepage;
}

I then extract the html code i need from the returned data using jQuery and insert it into a div called #BFX, as follows:

$("#btnNewLoadMethod1").click(function(){
  $('#temp1').load('pgiproxy.php', { data : $("#formdata").serialize(), mode : "graph"} , function() {
    $('#temp').html( $('#temp1').find('center').html() );
    $('#BFX').html( $('#temp').html() );
  });
});

This works fine. I get the html data (which is a gif image) i need displayed on screen in the correct div.

The problem is i can see the html data loading into the div (dependant on network speed), but what I want is to insert the extracted html code into #BFX ONLY when the ajax request has fully completed.

I have tried to use async:false and call $('#BFX').html( $('#temp').html() ); outside the load() function, this had the same effect.

+1  A: 

if you mean #temp1 is the div showing, .hide() then .show() it.

$("#btnNewLoadMethod1").click(function(){
  $('#temp1').load('pgiproxy.php', { data : $("#formdata").serialize(), mode : "graph"} , function() {
    $('#temp').html( $('#temp1').find('center').html() );
    $('#BFX').html( $('#temp').html() );
    // find img tags... then hide it.... when image finished loading, show it..
    $('#BFX').find('img').hide().load(function(){ $(this).show(); /* this function triggers when image finish loading... */})
  })
});

.load() for images... quick demo

code reduction maybe this will also work..

$("#btnNewLoadMethod1").click(function(){
  $('<div>').load('pgiproxy.php', { data : $("#formdata").serialize(), mode : "graph"} , function() {
    $(this).find('center img').appendTo('#BFX');
    // find img tags... then hide it.... when image finished loading, show it..
    $('#BFX').find('img').hide().load(function(){ $(this).show(); /* this function triggers when image finish loading...*/ })
  })
});

this is much less element usage...

Reigel
niczoom
@Nic " *i can see the html data loading into the div* "... when you say data, did you mean the image is seen loading... like part of the image is seen little by little depending on the speed of the net?
Reigel
yes that is correct. Have a quick read of my response to GerManson above.
niczoom
@Nic - see edited codes above...
Reigel
@Reigel - #temp1 (hidden div) already contains the IMG tag which is extracted using --$('#temp').html( $('#temp1').find('center').html() );-- , the load() function look promising, i had a quick read and will test it out after work.Can u explain the idea behind -- $('#BFX').find('img').hide().load(function(){ $(this).show(); }) -- , thanks for your help so far, Nic.
niczoom
added some comments on my codes for you nic... :)
Reigel
@Reigel - Thanks for your help, you pointed me in the right direction. I tweaked your code a bit and came up with a solution that worked exactly how i needed it to.
niczoom
just accept it as the answer... I'll be happy to help you again next time.. :)
Reigel
A: 

UPDATE: Getting the HTML for inserting an image on a page, its not the same as getting the file ready. with your current method, you are just asking for the HTML code (if i understood well), not for the .gif file.

if you append the new HTML into the page, then the browser is going to ask the server for the image.

You would need somehow, preload the image and then append the HTML code. You can search on google for several techniques on how to preload images.

=================================================================================

You can try with a normal jQuery Ajax call

Success function is executed when ajax has finished loading

$("#btnNewLoadMethod1").click(function(){
     $.ajax({
         url: "/image/path/file.gif",
         data: $("#formdata").serialize(),
         success: function() {
                 $('#temp1').load('pgiproxy.php', { data : $("#formdata").serialize(), mode : "graph"} , 
                 function() {
                     $('#temp').html( $('#temp1').find('center').html() );
                     $('#BFX').html( $('#temp').html() );
                 });
         }
     });
});
GerManson
where is `'#temp1'` now?
Reigel
I updated my answer.. can you please post the exact output of pgiproxy.php?
GerManson
hello GerManson, You are correct, I am just extracting the IMG tag from the HTML code which comes from an external webpage/site.I have tried this way: Once ajax request is complete and #temp (which is a Hidden div) contains the extracted IMG tag I then setup a button which when clicked copys the HTML into #BFX div, this works great, instant, no visual loading.So in essence the image has been preloaded into the hidden div #temp, but I need to display it in #BFX once the ajax call is FULLY complete. Hope this makes sense, Nic.
niczoom
The output of pgiproxy.php is just the full HTML code of a webpage, have a look here "http://www.liamharding.com/admartnew/temp111.php"
niczoom
That address i just gave is just an example, i dont get the webpage from there. ALso, u have a local path to an image, url: "/image/path/file.gif, the url I pass is an external website.
niczoom
A: 

Thanks to @Reigel for his help, I tweaked his code to work as I needed:

$("#btnNewLoadMethod2").click(function(){
  $('<div>').load('pgiproxy.php', { data : $("#formdata").serialize(), mode : "graph"} , function() {
    $('#temp').html( $('center img', this) );
    $('#temp').find('img').load(function(){ $('#BFX').html($('#temp').html() ) })
  })
});

The HTML received from the Ajax call to pgiproxy.php is stored in a jQuery object $('<div>').

Using $('#temp').html( $('center img', this) ); I extracted the HTML that I needed and stored it in a div named #temp.

The final line:
$('#temp').find('img').load(function(){ $('#BFX').html($('#temp').html() ) }) binds a load event to the IMG element which runs the function $('#BFX').html($('#temp').html() once the image has completed loading, this function just copies the HTML from #temp div (which is hidden) into my main div #BFX.

Now when the user loads each successive image all they see is the new image just appear over the top of the old one. Since the images are of a market graph, the graph line appears to be drawing along the image.

Hope I explained it clearly enough, Thanks again for the help.

niczoom