Parse xml into iframe
I want to be able to access rss feed from js. I have the option to configure both servers to use the same domain (but different subdomains - for example static.benanderson.us and tech.benanderson.us). I was hoping I could use the document.domain property to get around the xss issue. Here is a snippet from http://static.benanderson.us/example.js (not actually live):
document.domain = 'benanderson.us';
new Ajax.Request('http://tech.benanderson.us/feeds/posts/default', { \\error
However, this doesn't work. I couldn't find out if document.domain was working for xhr requests, so I wrapped that up and switched to an iframe solution because I've done something similar in the past.
$('my_iframe').src='http://tech.benanderson.us/feeds/posts/default';
Event.observe($('my_iframe'), 'load', function() {
try {
log(this.contentDocument); //this displays just fine
var entries = this.contentDocument.getElementsByTagName('entry'); //error
The strange thing is that I can view this.contentDocument in firebug, however it is getElementsByTagName that errors with the message "rejected ...".
Any thoughts on how to get any of these solutions to work would be awesome. I know I can do a proxy - that's not what I'm interested in.
source to share
there is obviously no way to do exactly that. Anyway, I was able to come up with a decent solution. The rss xml comes from blogger (tech.benanderson.us). So I added a javascript function there that could turn xhr into rss. This javascript then sets its document.domain to benanderson.us and invokes a callback. Summarizing:
http://static.benanderson.us/example.js :
Event.observe(window, 'load', function() {
document.domain = 'benanderson.us';
$('my_iframe').src='http://tech.benanderson.us/2001/01/js.html';
});
function renderFeed(feedXml) {
...
http://tech.benanderson.us/2001/01/js.html :
var url = 'http://tech.benanderson.us/feeds/posts/default';
new Ajax.Request(url, {
method: 'get',
onSuccess: function(response) {
document.domain = 'benanderson.us';
top.renderFeed(response.responseXML);
}
});
source to share
The problem is that document.domain needs to be set in benanderson.us on both the page that loads the iframe and the page in the iframe. It gets a little silly in this case, as you can't just put javascript in the rss feed, so you probably have to make some kind of gateway page in the same subdomain in order to load that page in a frame for you. Here's a lazy example:
<html>
<frameset onload="document.domain='benanderson.us';window.frames['content_frame'].location=location.href.split('?request=')[1]">
<frame name=content_frame onload="window.frames['content_frame'].document.domain='benanderson.us'">
</frameset>
</html>
So, assuming we call this "gateway.html" and you put this somewhere inside the tech.benanderson.us subdomain, you should go "gateway.html? Request = http://tech.benanderson.us/feeds / posts / default "So, in this case you will need to link to it via window.frames [" my_frame "]. window.frames ["content_frame"] and you should get it.
NOTE. I have not tested this code.
source to share
Below is actually working code, parseXML is my wrapper around DOMXML, so you can use window.frames ["internal"] instead. document as an XML object. This works in Firefox and Opera. "this" doesn't work because "this" is an "Element" iFrame, not a frame. Sorry for the language, but you get the idea.
document.getElementById("internal").onload=function() {
//wrap xml for easy usage
var ret=parseXML(window.frames["internal"].document);
//show what happened
showResult(ret, [
new DataPair("başlık", "Eklenti Ekle"),
new DataPair("nesne" , "Eklenti")
]);
//no error
if(ret.findNodeValue("error")=="no") {
//close
eklentiEkleKapat();
//Protection from refresh
document.getElementById("internal").onload=function() {}
window.frames["internal"].location.href="about:blank";
//activate attachments tab
tab_eklentiler.activate(true);
}
}
//I actually use a form to post here
document.getElementById("internal").location.href="target.php";
source to share