I have a simple site with header, left menu and main content.
I already made it with one static page (index.php)
and then use jquery
to change main content by clicking on menu item:
$('#divR').load('chapters/abc.php');
I was advised somewhere that I should change this arrangement (without explanation - WHY) using separate page for each main content, and then using php include
to include header and left menu on each page.
Could someone help with this dilemma ?
both loading with ajax or including the page in php will work. However, for displaying a new included page you need a full page refresh. If you request the page with jquery it will load asynchronous, without page refresh.
keeping that in mind you have to choose the best suited option to enhance the user experience. I never use ajax to load a full page because there are rumours that google does not index those asynchronous links. However some people say google bot is a sort of full capable browser not skipping ajax calls. I don't know, so I keep it save and load important "main" content through a full page refresh, visiting an other url. This also will make the browsers back and next (history) buttons work. It will not with ajax calls because they are asynchronous.
for example, I use ajax to load new thumbnails for my image gallery, or new entries to (re)populate a table, or showing usermessages, or update some variables,...
I use <?php include "file.html"; ?>
VERY often now, because it just has some advantages over jQuery .load
.
I have a lot of other scripts on the website and somehow, if I use .load
, the jQuery that is interacting with this content doesnt work because it is loaded afterwards I think. Even though it is in the <head>
and you don't use $(document).ready(function() { CODE });
and it should be loaded at first...
But if you use <?php include "file.html"; ?>
, the content will be loaded before your jQuery loads so the javascript can interact with the content that is loaded in this file.
I don't know how to explain, but at least it doesn't work for me as it should, maybe because php
is a serverside language.