M
M
Magzhan Birgebayuly2015-10-01 18:33:47
PHP
Magzhan Birgebayuly, 2015-10-01 18:33:47

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); }
  });
});


And the index.php file itself:
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

1 answer(s)
D
Dmitry Korolev, 2015-10-01
@Apathetic

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 question

Ask a Question

731 491 924 answers to any question