I try to implement pushstate history on my website in order to load content from a single.php page inside a index.php container.
My website have two main page : index.php and single.php .
On the index.php there are links that call pushstate script:
<a class="phplink" href="/Formlabs-3D-printer" title="Formlabs 3D printer">Post 12</a>
<a class="phplink" href="/Minimal-Bumper-for-iPhone-5" title="Minimal Bumper for iPhone 5">Post 11</a>
On my single.php page I use isset get method to dynamically load content that correspond to clicked link on index.php:
<?php
if (isset($_GET["page"])) {
//I do some stuff in order to echo content
?>
In my .htaccess file I rewrite the url link
Options +FollowSymLinks
RewriteEngine on
RewriteRule /([a-zA-Z0-9\-]+)$ /index.php
RewriteRule /([a-zA-Z0-9\-]+)$ /single.php?page=$1 [L]
And here my pushstate script:
$.ajaxSetup({cache:false});
$(".phplink").click(function(){
var $post_link = $(this);
load_content($post_link.attr('title'),$post_link.attr('href'));
return false;
});
window.onpopstate = function(event) {
if (event.state) {
load_content(event.state.title, window.location.pathname, true);
} else {
var stateObj = {
title: document.title,
url: window.location.pathname,
};
url = window.location.pathname;
load_content(url,url);
}
}
function load_content(title,url,skipHistory) {
$.get(url,function (data) {
document.title = title;
var stateObj = {
title: title,
url: url
};
if (!skipHistory) {
if (typeof window.history.pushState == 'function') {
window.history.pushState(stateObj,title,url);
}
}
if(url.substring(1) != '') {
$("#ajaxify_container").html("loading...");
$("#ajaxify_container").load('single.php?page='+url.substring(1)+' #container-2');
}
else {
$("#ajaxify_container").html('');
}
});
}
My pushstate script works to load content on link click (on .phplink click). It works also for back/forward button.
1st PROBLEM : When I refresh the browser (with an pushstate link in the url) it works on google chrome (load content from single.php to index.php container) but no in IE10 (nothing is loaded, it stay on index.php page).
2nd PROBLEM : If I disable javascript to see what's happen for googlebot (for SEO). I can't load/reach the single.php page, it always stay on index.php. So single.php page can't be crawled by search engine (I suppose, but I'm not sure about this) . This behaviour is normal because I set in my .htaccess file, that "all this links" will be redirect to index.php .
I do this trick because without it pushstate loads single.php page when I refresh. And I don't want this behaviour. I want when I refresh it just load content from single.php into index.php container.
So my main problem (problem 2) is: I don't know how to write my script or my links in my php page in order to load content in my index.php file when I click, I refresh and I back/forward.
In normal behavior of pushstate, does on browser refresh, onpopstate can load content from a page into a container of an other page (load content from single.php into a container of index.php) ?
I hope that someone can help me and explain how it works. I have some difficulty to understand how it work with links.