问题
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 ?
回答1:
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*/
}
来源:https://stackoverflow.com/questions/28766267/how-can-i-check-if-html-content-is-called-by-ajax-request