Hi people!
I'm experimenting on making the
old legacy template work entirely using
Ajax/XHR requests, to avoid reload/refreshing/loading a new page. Here is my current work (download the old template
here, or use the default template of
HFS v2.3m, and replace the 'body' with the following code):
<body>
<!--{.comment|--><h1 style='margin-bottom:100em'>WARNING: this template is only to be used with HFS 2.3 (and macros enabled)</h1> <!--.} -->
{.$box panel.}
<div id="list-panel"></div>
<script type="text/javascript">
var xhr = new XMLHttpRequest();
xhr.open("GET", "/~list");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onload = function() {document.getElementById("list-panel").innerHTML = xhr.responseText};
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
alert("XHR request OK");
}
if (xhr.readyState == 4 && xhr.status == 401) {
alert("XHR request fail");
}
}
xhr.send();
</script>
</body>
And here is a shorter version:
<body>
<!--{.comment|--><h1 style='margin-bottom:100em'>WARNING: this template is only to be used with HFS 2.3 (and macros enabled)</h1> <!--.} -->
{.$box panel.}
<div id="list-panel"></div>
<script type="text/javascript">
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("list-panel").innerHTML = this.responseText;
alert("XHR request OK");
}
};
xhr.open("GET", "/~list", true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send();
</script>
</body>
The XHR request work (I'm temporally using an '
alert' to be sure), but the files are not show. I'm sure my code is wrong. The %list% variable fails (doesn't get executed, since, if I'm not mistaken, %list% is meant to run on the server side, but the XHR is run on the client side). I don't know what to change to make this work, but I'm open to listen your suggestions...
This was requested and discussed a long time ago (
here), and there Rejetto suggested a way more elegant (shorter) Ajax code. So, feel free to suggest a more pure Ajax-jQuery code, as long it work on old browsers (to support the same old browsers that this old template supported). Please avoid using ES6 (like 'promises', or anything not supported by legacy browsers). My objective is to use JavaScript code compatible with jQuery v1.4.2 (or the most "pure" plain
vanilla JavaScript as possible).
I don't want to redesign the old template, only to make it entirely run on Ajax, so the changes must be as minimal as possible. Anyone feel free to collaborate, this post is open to everyone.
Cheers,
Leo.-