Well, I want to use the JSON-to-DOM parser but I just don't really know how integrate all that in my script... Like, does JSON-to-DOM has to be on page B or when I call AJAX?
It has to be on page A. You use XMLHttpRequest (this isn't AJAX, there's no XML involved) to retrieve the content from your server-side script, evaluate it, then call jsonToDom() on the resulting object, before appending it to the <div>, or perhaps we should replace the <div> entirely.
And what are the "[" and "]" before and after the vars to fetch the js?
Square brackets in Javascript indicate an array. If we were to replace the <div>, however, that wouldn't be necessary.
Code:
// xhr.js
var XHR = {
jsonToDom: function jsonToDom(o) {
if (typeof o === "string")
return document.createTextNode(o);
var r = document.createElement(o._tag);
for (var x in o)
if (o.hasOwnProperty(x)
&& x.indexOf("_") !== 0)
r[x] = o[x];
if(o._children && o._children.length)
for (var i = 0, e = o._children, n = e.length; i < n; ++i)
r.appendChild(jsonToDom(e[i]));
return r;
},
getXHR: function() {
if(typeof XMLHttpRequest === "function")
return new XMLHttpRequest();
else if(ActiveXObject)
try {
return new ActiveXObject("Msxml2.XMLHTTP");
} catch (e1) {
try {
return new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
return null;
}
}
else return null;
},
updateElement: function(url, element) {
var xhr = XHR.getXHR();
xhr.open("GET", url, true);
xhr.onreadystatechange = function() {
if(this.readyState === 4 && this.status === 200) {
element.parentNode.replaceChild(jsonToDom(eval("(" + xhr.responseText + ")")), element);
xhr = element = null;
}
};
xhr.send(null);
}
};
Code:
<!-- Page A -->
<style type="text/css">
iframe.alpha-frame {
position: absolute;
left: -9999%;
}
</style>
<script type="text/javascript" src="xhr.js"></script>
<script type="text/javascript" src="alpha.js"></script>
</head>
<body>
<p>
<!-- Your previous use of <br> was an abuse. -->
<a
href="up/load"
onclick="XHR.updateElement(this.href + '?xhr=true', document.getElementById('ajx')); return false;">
Upload
</a>
<div id="ajx"></div>
Note: up/load should return a full HTML page if accessed directly (without the GET "xhr" parameter) for non-Javascript users.
Code:
##> Page B <###
{"_tag" : "div",
"id" : "ajx",
"_children" : [
{"_tag" : "script",
"type" : "text/javascript",
"_children" : [
"var ext_allowed = '<TMPL_VAR ext_allowed>';",
"var ext_not_allowed = '<TMPL_VAR ext_not_allowed>';",
"var max_files = <TMPL_VAR max_files>;",
"var max_size = <TMPL_VAR max_size>;",
"var descr_mode = <TMPL_VAR enable_file_descr>;"]},
{"_tag" : "iframe",
"src" : "about:blank",
"name" : "alpha",
"className" : "alpha-frame"},
{"_tag" : "script",
"type" : "text/javascript",
"_children" : ["InitSelector();"]}]}
Bookmarks