
Sometimes we have found the need to provide load page via ajax elements. It is important to build a fully functional website without Javascript SEO and not penalize users who do not use Javascript. Temptation is requested through AJAX the overall content of the page to load and then let Jquery selectors filter information by actually going to upgrade in the web. This leads to unnecessary excess traffic, as we will apply the entire contents of the page to use just one piece of it, because normally the header, menu, etc. .. are the same for each of the pages. To solve this we evaluated two options:
- Build a php file return only the information we need.
- Parse PHP output directly to the network just send the necessary information.
The first case is optimal in terms of traffic load and generates only the necessary information and generates no extra query to the database, it has one drawback: the maintenance, any change in the application logic should be done in duplicate in the conventional web script that sends the AJAX part. And this is a problem especially in large applications. The second case, that solves this problem and based on the same code and with the passage of a variable get, PHP only returns the necessary contended eliminating unnecessary traffic. If we use some form of caching PHP output, as in our case, we eliminate the extra queries to compose the rest of the page that we will not need. Penalize only the memory and CPU load required for server parsing of XHTML. We opted for the second option and for this we use http://simplehtmldom.sourceforge.net/ a tool that allows us to work with the DOM in a similar way as we do with JQuery. In our PHP just have to add the following lines:
Where $ globalHTML is the variable that contains all the HTML of the páginba, we usually use Smarty, so retrieve the HTML before sending it to the output is very simple using fetch instead of display and in our javascript
- ) . load ( "nuestraPagina.php?selector=#seccionACargar" ) ; $ ("# SeccionACargar.") Load ("nuestraPagina.php? Selector = # seccionACargar");
Thus nuestraPagina.php loading? Selector = # seccionACargar the php will not return all the HTML of the page if not we will return only the content of saving a good candidad selector byte (or Kb) in the transmission of data. As I said this is a huge savings over the alternative offered jquery
- ) . load ( "nuestraPagina.php #seccionACargar" ) ; $ ("# SeccionACargar.") Load ("nuestraPagina.php # seccionACargar");
Jquery recovered since the entire page and then it parses. We have successfully used this technique in www.berroguetto.com to load each of the sections of the website, and item of them, like news and each of the pages of the pagers.









