0

Here is the HTML content of a mosaic of images.

<div id="container">
  <img id="1" />
    ...
  <img id="20" />
</div>

This script is creating a mosaic from the images after page has been loaded or resized.

<script>
function create_mosaic(){
  /*creates mosaic by resizing and arranging images in container*/
}

jQuery(window).load(function() {
  create_mosaic();
});
jQuery(window).resize(function() {
  create_mosaic();
});
</script>

However, if the HTML content requested by AJAX request, this script cannot work, since window.load is not being triggered. For this case, I am thinking about calling create_mosaic() function after all images have been loaded. For that purpose, how can I check if the HTML is requested by AJAX request ?

progmastery
  • 243
  • 3
  • 10
  • you could pass an argument or create a separate function that does the logic for one separately. – Daniel A. White Feb 27 '15 at 13:30
  • 3
    This question doesn't make much sense. Where are you trying to figure out if the HTML is loaded by AJAX? – Phylogenesis Feb 27 '15 at 13:31
  • I cannot control the AJAX request, I have to check this from inside my script. – progmastery Feb 27 '15 at 13:32
  • You could capture onload event on browsers which support capturing phase for all images to handle the dynamic ones. That's said, that's not completly clear what you are asking. – A. Wolff Feb 27 '15 at 13:33
  • You cannot force code to be run from the 'sub page'. This would be a significant security issue. – Phylogenesis Feb 27 '15 at 13:33
  • Question is its current for makes no sense at all. It would make a whole lot more sense if it were rephrased something like, "how do I create content from data loaded via AJAX?" – Roamer-1888 Feb 27 '15 at 13:40
  • What about providing relevant code regarding these ajax requests? Why not using relevant request callback to call `create_mosaic()` method? – A. Wolff Feb 27 '15 at 13:46
  • From what I can tell, @progmastery has no control over the page that is loading his content via AJAX, but wants to be able to trigger javascript to be run in the calling page. This just isn't possible. – Phylogenesis Feb 27 '15 at 13:47

1 Answers1

0

Someone answered the question, but deleted it then. I tried, and it works! Here is the way:

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
 strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
   /** Yes, it was an ajax call! */ 
   /* I can call create_mosaic() function here 
     after checking if images have been loaded*/
}
progmastery
  • 243
  • 3
  • 10