I have serious doubts about that method's applicability in this and pretty much any situation. You cannot call it via AJAX to get the value because the javascript on it cannot be used under an AJAX request, it requires a reload of the page so sort of defeats the purpose of doing anything unobtrusively, because it's using the hash, many requests to it will not be passed to the server anyway, and owing to its reliance upon cookies adds layers of uncertainty as to whether or not it will perform as expected.
That said, it doesn't work even as stated on the page because the code is written using invalid characters and is poorly thought out as well. If it would be of any help, here's a working version:
PHP Code:
<script type="text/javascript">
(function(){
var h = location.hash;
h = h.substring(h.indexOf('#') + 1);
document.cookie = 'anchor=' + h;
<?php
$c = isset($_COOKIE['anchor'])? $_COOKIE['anchor'] : '';
$c = "'" . $c . "'";
?>
if(h !== <?php echo $c; ?>){
location.reload();
}
})();
</script>
<?php
$c = isset($_COOKIE['anchor'])? $_COOKIE['anchor'] : '';
echo $c;
?>
Perhaps if you ran it in a hidden iframe . . . But then there would be caching issues added to those already mentioned. These could perhaps be worked around, and then timing would become critical as to when this value became accessible to the rest of your code, still not solving many of the other issues.
But I would strongly advise you to use a GET request with a valid query string for this, it would be so much simpler.
Bookmarks