tags:

views:

94

answers:

1

Based on Please explain JSONP, I understand that JSONP can be used to get around the same-origin policy.

But in order to do that, the page must use a <script> tag.

I know that pages can dynamically emit new script tags, such as with:

<script type="text/javascript" language='javascript'>

  document.write('<script type="text/javascript" ' + 
                 'id="contentloadtag" defer="defer" ' +
                 'src="javascript:void(0)"><\/script>');
  var contentloadtag=document.getElementById("contentloadtag");
  contentloadtag.onreadystatechange=function(){
    if (this.readyState=="complete") { init(); }
  }
</script>

(the above works in IE, don't think it works in FF).

... but does this mean, effectively, that every JSONP call requires me to emit another <script> tag into the document? Can I remove the <script> tags that are done?

+1  A: 

Yes, every request yields a new <script> tag, and yes, you can remove <script> tags when you're done using the data that it provides to you.

You should consider using a Javascript library for JSONP. OX.AJAST is a simple library I wrote some time ago for doing asynchronous request through script tags (i.e. JSONP) across browsers. YUI also supports JSONP if you're already using that.

Håvard S
Do those lirbaries you mentioned remove the `<script>` tags automatically? or do I need to do that manually?
Cheeso
OX.AJAST does it, I don't know if YUI does it.
Håvard S