Skip to content Skip to sidebar Skip to footer

Parse Xml In An Iframe

I want to be able access an rss feed from js. I have the ability to configure both servers to use the same domain (but different subdomains - eg static.benanderson.us and tech.ben

Solution 1:

This doesn't speak to the JS technicalities at all, but as a workaround, you could set up a server-side script on the same subdomain that just fetches what you need from the other subdomain.

Solution 2:

apparently there is no way to do exactly this. Howver, I was able to come up with a decent solution. The rss xml is coming from blogger (tech.benanderson.us). So I added a javascript function there that could make the xhr to the rss. Then this javascript sets it's document.domain to benanderson.us and makes a callback. To sum up:

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';
});
functionrenderFeed(feedXml) {
  ...

http://tech.benanderson.us/2001/01/js.html:

var url = 'http://tech.benanderson.us/feeds/posts/default';
newAjax.Request(url, {
  method: 'get',
  onSuccess: function(response) {
    document.domain = 'benanderson.us';
    top.renderFeed(response.responseXML);
  }
});

Solution 3:

You cant do that, it is not allowed by same origin policy

You can only set document.domain to the superdomain of the current domain, you do that but the same origin policy has to match the whole domain name that it is allowed (tech.benanderson.us != benanderson.us)

Solution 4:

The problem is that document.domain needs to be set to benanderson.us both on the page loading the iframe and the page in the iframe. That gets a bit stupid in this case since you can't just put javascript into a rss feed, so you'll probably have to make some kind of gateway page on the same subdomain to load that page in a frame for you. Here's a lazy example of that:

<html><framesetonload="document.domain='benanderson.us';window.frames['content_frame'].location=location.href.split('?request=')[1]"><framename=content_frameonload="window.frames['content_frame'].document.domain='benanderson.us'"></frameset></html>

So, assuming we call this "gateway.html" and you put this someplace inside the tech.benanderson.us subdomain, you would go "gateway.html?request=http://tech.benanderson.us/feeds/posts/default" So, in this case, you would have to reference it through window.frames["my_frame"].window.frames["content_frame"] and you should get it.

NOTE: I haven't tested this code.

Post a Comment for "Parse Xml In An Iframe"