开发者

How do you remove the script tag added by cross-domain ajax?

开发者 https://www.devze.com 2023-01-02 13:33 出处:网络
I am using the JSONP/dynamic-script-tag technique to perform cross-domain AJAX (There\'s no XML, but you know what I mean).

I am using the JSONP/dynamic-script-tag technique to perform cross-domain AJAX (There's no XML, but you know what I mean).

Initially, I wrote my own solution, but I could not come up with an elegant way to remove the script after it executed. My strategy was just to pass an ID and on the callback remove the associated script, but I realized this would prevent caching, which I do not want to do.

That went something like:

1) Dynamically insert: <script src="http://example.com/handler.php?callback=x&scriptid=y"></script>.

开发者_如何学JAVA2) The script loads and runs x(); removeScript(y); where removeScript took the appropriate script element out of the head element.

It worked great but ruined caching. So I was excited to learn jQuery provides a jsonp method and quickly implemented it, figuring they had got this all figured out. Instead jQuery leaves the script element there.

Is there a clean way to remove these elements?


Thanks to your question I discovered this project on code.google: jquery-jsonp . this plugin provides some functionality that jquery does not.

I never used it but seems to be cool and handle a great variety of things, including caching. You can find some examples of its usage here: http://code.google.com/p/jquery-jsonp/wiki/TipsAndTricks


Why would you want to remove them? Only web geeks look at the source code of a page, and chances are you won't get paid by web geeks to make a website. They're harmless, so you don't need to go all the trouble to remove the tags.

0

精彩评论

暂无评论...
验证码 换一张
取 消