I have this javascript which fetches a html part from the server. I'm able to return the html in an alert message and it's the correct code.
ajax: function(url){
var request = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
.createInstance(Components.interfaces.nsIXMLHttpRequest);
request.onreadystatechange = function() {
if (request.readyState == 4) {
// only if "OK"
if (request.status == 200) {
var popup = document.getElementById("ajax"); // a <menupopup> element
var txt = request.responseText;
alert(txt);
popup.appendChild(txt);
} else {
alert("There was a problem retrieving the XML data:\n" +
request.statusText);
}
}
};
request.open("GET", url, true);
request.send(null);
}
the xul/html returned from the server:
<menuitem>
<html:h2><html:a href="http://google.com">Google</html:a></html:h2>
<html:p><html:table><html:tr><html:td>xxxx</html:td></html:tr></html:table></html:p>
</menuitem>
<menuitem>
<html:h2><html:a href="http://yahoo.com">Yahoo</html:a></html:h2>
<html:p><html:table><html:tr><html:td>yyyy</html:td></html:tr></html:table></html:p>
</menuitem>
My xul page:
... some more code
<menu class="menu" label="test">
<menupopup id="ajax" width="450" height="700" onpopupshowing="myextension.ajax('http://www.myserver.com/phpscript.php'>
</menupopup>
</menu>
Now if I attach the html directly to the menupopup with id ajax it works as expected. When I attach it using appendChild it doesn't. I know I can't use appendChild but there seems to be no equivalent to innerHTML in XUL. I have no control over the xul/html returned from the server so I can't use the DOM methods to add the content and bypass the html.
I tried using HTMLParser https://developer.mozilla.org/en/Code_snippets/HTML_to_DOM to convert it into a DOM object but that doesn't seem to work, I believe because it cuts of the <menuitem>
tag.
Any ideas how to attach the HTML to a menupopup so I can display them as menuitems.
Ok I tried the iframe approach:
ajax: function(url){
var request = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
.createInstance(Components.interfaces.nsIXMLHttpRequest);
request.onreadystatechange = function() {
if (request.readyState == 4) {
// only if "OK"
if (request.status == 200) {
var frame = document.getElementById("frame");
frame.setAttribute("src", "data:text/html," + encodeURIComponent(request.responseText));
} else {
alert("There was a problem retrieving the XML data:\n" + request.statusText);
}
}
};
request.open("GET", url, true);
request.send(null);
}
with
<iframe id="frame" type="content" src="" />
This doesn't seem to work. However if I do an alert of the encodeURIComponent(request.responseText) it's correctly encoded. Evenmore if I directly add it to the iframe like this:
<iframe type="content" src="data:text/html,%3Chtml%3E%0A%3Cbody%3E%0A%3Cp%3EHello%20everyone%3C%2Fp%3E%0A%09%3C%2Fbody%3E%0A%3C%2Fhtml%3E%0A%0A"/>
it does not work because to display the html it needs to be within tags but the html contains many different items which should be each in their own tag. Simply adding to the html is not working.