How can I check if HTML content is called by AJAX request?

元气小坏坏 提交于 2019-12-08 10:44:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!