Answer the question
In order to leave comments, you need to log in
Content loading script using Ajax, JavaScript (location.hash) and PHP. What are its disadvantages?
JavaScript:
$(window).on('hashchange', function(){
var u = location.hash.replace('#/','');
if( u == '' ) return false;
$.ajax({
url: '/?src='+u, dataType: 'html', async: false,
success: function(html){ $('#content').html(html); }
});
});
if( empty($_GET['src']) ){ include 'template.php'; exit; }
$src = $_GET['src'];
while( $src[0] == '/' ) $src = substr( $src, 1, strlen($src) );
while( substr( $src, strlen($src)-1, 1 ) == '/' ) $src = substr( $src, 0, strlen($src)-1 );
$src = explode('/', $src);
$f = 'pages/'.$src[0].'.php';
if( is_file($f) ){ include $f; exit; } // file
include 'pages/404.php'; exit; // 404 not found
Answer the question
In order to leave comments, you need to log in
The main disadvantage is that with disabled JS, the content cannot be seen in any way.
The remaining problems are, in general, typical for such solutions (hashbang in particular): lack of semantics, problems with sharing in social networks, problems with indexing, and so on.
In fact, two things need to be solved: the first is to immediately give the necessary content when clicking on the link, without any Ajaxes. The second is to build normal URLs, without any left hashes and other hacks. To do this, there is the History API, which, in principle, is already well supported ( caniuse.com/history).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question