In order to load some geojson data, I need to source scripts on an external domain, say http://www.stat.ucla.edu/~jeroen/files/la_county_simplified.min.json
. I have no control over the contents of this script; all I know is the url, and the name of an object defined in the script that I am interested in. A dummy version of the script looks like:
var my_data = {"foo" : 123, "bar" : 456}
Now in my application, I would like to load the my_data
object dynamically from its URL. Because it is cross domain, I can't use ajax. It isn't quite jsonp
either, because my script defines an object, not a function. One way would be to insert it simply as a <script>
in the head of the current document. However, I would like to avoid possible side effects.
What would be a cleaner solution? I was thinking of creating an <iframe>
and then inserting the <script>
tag in the iframe, and extracting the object once the iframe has loaded. However I am not sure this is a reliable method that will work cross browsers (especially binding a callback to extract the object after the script has been loaded in the iframe).
Is there some library or standard solution to load a script in a clean page, and extract copy over a particular object to the main page? I already have a dependency on jQuery so that would be fine.