Forum How do I...?

asynchronous JS content no rendered

dsusco
I have some JS that fetches some cross-domain JSON and generates content on an HTML page. When I render with Prince the content doesn't show, I'm assuming because Prince doesn't wait for the JSON to be fetched and processed.

Any suggestions on what to do here?
mikeday
Prince doesn't officially support XMLHttpRequest yet, although there are some initial stubs for it. So the JSON request may never come back. Can you load it some other way, eg. using JSONP?
dsusco
I am using JSONP, it's a jQuery getJSON call:

http://api.jquery.com/jquery.getjson/

My URL includes the callback=? that makes it JSONP.
mikeday
This will probably require creating a <script> element directly using the DOM, as it looks like jQuery uses features of XMLHttpRequest we have not implemented yet.
dsusco
So my callback should be creating a script tag that contains what's returned, and executes it?
mikeday
Currently in Prince you can do something like this:
var s = document.createElement("script");
s.src = "http://example.com/my.js";
document.body.appendChild(s);

Then the my.js file will be retrieved and executed, so it can call your callback function and pass the JSON to it. This avoids having to use XMLHttpRequest.
dsusco
Got around to trying this today. I'm getting an "error: Couldn't resolve host '[my host here]'"

I've been meaning to ask about this one before, how can I get external files? For instance, I normally use jQuery via a CDN, but I haven't found out how to do that with Prince.
mikeday
Regular HTTP URLs should just work. If you are getting DNS errors, it could be that you need to install a different Prince package. Are you running Prince on Linux?
dsusco
They weren't working on r3, I updated to r4 and I'm good. Thanks.
ybochatay
Do you know when Prince will support XMLHttpRequest ? It's really a pity it doesn't.
mikeday
Perhaps next year.