Here, less example guff that lets us get to the meat so we can see what it all does.
PHP Code:
<?php
// Template, Pure HTML Output
$output_html = <<<HTMLEOF
<!doctype html>
<!-- Lets not bother with the XHTML nonsense -->
<html>
<head>
<title>{:title:}<title>
<script>
function changeScript ( s ) {
var o = document.getElementsByTagName( "script" )[0],
n = document.createElement( "script" );
n.setAttribute( "src", s ); // type="text/javascript" is implied.
o.parentNode.replaceChild( n, o );
};
function init () {
var a = document.getElementsByTagName( "a" ),
i = 0;
for ( i; i < a.length; i++ ) {
a[ i ].onclick = function () {
if ( this.href.indexOf( "?" ) > -1 )
changeScript( this.href + "&live=1" );
else
changeScript( this.href + "?live=1" );
return false;
}
}
}
windows.onload = init();
</script>
</head>
<body>
<div id="main_content">
{:body_content:}
</div>
</body>
</html>
HTMLEOF;
#-------------------------------------------------------------------------------
// Template, Javascript Output
$output_js = 'document.getElementById( "{:container:}" ).innerHTML="{:body_content:}";init();';
#-------------------------------------------------------------------------------
$send_js = false;
if ( isset( $_GET[ 'live' ] ) )
$send_js = (bool)$_GET[ 'live' ]; // Lets keep it simple for now.
$title = 'Uninteresting title';
$content = 'Mary Poppins was here, <a href="?go=Supercalifragilisticexpialidocious">Supercalifragilisticexpialidocious</a>!!!';
#-------------------------------------------------------------------------------
// Javascript only output
if ( $send_js === true ) {
header( 'content-type: application/x-javascript' );
echo str_replace(
[ '{:container:}', '{:body_content:}' ], // PHP 5.4 Array Sugar Syntax
[ 'main_content', str_replace( '"', '\\"', $content ) ],
$output_js
);
exit;
}
#-------------------------------------------------------------------------------
// No Javascript
echo str_replace(
[ '{:title:}', '{:body_content:}' ],
[ $title, $content ],
$output_html
);
But you know you should just use XMLHttpRequest and pull in JSON...its a lot safer and easier. Almost all languages out there have a JSON en/decoder. (Despite its name XMLHttpRequest is not limited to only XML.)
Bookmarks