I got a couple comments and emails about my post on cross sub domain JS and AJAX and using an iframe to make that happen. As I was making an example of it so the post made more sense, I thought about the cross sub domain issues we were having at work, and the “duh” solution to the issue of passing data between sub domains. Use JSON instead of XML. The only time you couldn’t use JSON is if you were trying to get a full HTML page instead.
Its absurdly easy with jQuery and there are no iframes or anything else to mess around with. Just setup your js as a function call with a JSON object inside of it with your data.
myData({ "title":"test"})
Then on your page where you want the data, after adding jQuery just run the following
<script type="text/javascript">
function myData(theData) {
alert(theData.title);
}
$.getJSON("http://test.tomhoppe.com/test.js?callback=?");
</script>
This will grab the test.js file containing the function callback and execute that function on your page. At that point you can do whatever you please with your data. Only do this with trusted sources of course, as the JS that you pull in gets immediately executed on your own page.
Hi,
Don’t know if you can help me, but I’m trying to do a variation of what you’ve displayed on this page. (somewhat;)
I want to allow any name entered as a subdomain (easy enough and that’s not the issue)
My question is I want to be able to CAPTURE the whatever subdomain that was entered, then store it to personalize content of the page.
any help would be greatly appreciated
Annick