JSONP Memory Leak
27 July 2009
[Sorry, this is a technical post. Non-programmers should probably skip this and listen to some nice accordion music instead.]
Occasionally one has to defeat the same-origin policy and exchange data from a 3rd party server. The least ugly hack is the badly named JSONP technique. Basically you create a script tag, append it to head, then it will fetch a remote JavaScript file and execute it:
script = document.createElement('script');
script.src = 'http://example.com/cgi-bin/jsonp?q=What+is+the+meaning+of+life%3F';
script.id = 'JSONP';
script.type = 'text/javascript';
script.charset = 'utf-8';
// Add the script to the head, upon which it will load and execute.
var head = document.getElementsByTagName('head')[0];
head.appendChild(script);
This works well and can be executed at any time, not just at page load. As soon as the remote script is loaded it will execute will full permissions. Data can be sent to the server as in GET parameters on the script URL, and data can be received as JavaScript data structures in the returned file. One catch is that URLs have a maximum length, one that is not standardized. Thus if one wants to send more than about a thousand characters, then one needs to break the call into multiple requests.
When run once or twice, this is all fine. But when run repeatedly, the head will fill up with old script tags. Deleting them would appear to be easy:
// Remove any old script tags.
var script;
while (script = document.getElementById('JSONP')) {
script.parentNode.removeChild(script);
}
The above code will successfully remove the script tags from the DOM. The problem is that every one of today's browsers will fail to garbage collect the offending node. The DOM and the JavaScript world have separate garbage collection algorithms and an entity which has one foot in both is often immortal since each side is scared of corrupting the other side. Thus the script tags stay in memory.
Since my application involves transmitting significant volumes of data through JSONP, the result was a browser leak in excess of 15 MB per hour. Leave a few of those tabs open overnight at your own risk. The solution was pretty simple, accept that the script tag will last forever, but destroy all its properties:
// Remove any old script tags.
var script;
while (script = document.getElementById('JSONP')) {
script.parentNode.removeChild(script);
// Browsers won't garbage collect this object.
// So castrate it to avoid a major memory leak.
for (var prop in script) {
delete script[prop];
}
}
A couple of lines of code saved many terabytes of user memory per day. Sweet.
As usual IE is the odd browser which requires a special case. IE doesn't like deleting native properties off of DOM nodes. Fortunately this doesn't matter, since IE allows one to reuse script tags. Just change the SRC property and it will fetch the new page immediately. Thus one only needs a single script tag in IE.
分享到:
相关推荐
JSONP(JSON with Padding)是一种跨域数据交互协议,它利用了`<script>`标签可以跨域请求资源的特性,广泛应用于Web应用中获取数据。然而,这种技术也带来了一定的安全隐患,尤其是在处理敏感信息时,可能会导致...
JSON(JavaScript Object Notation)和JSONP(JSON with Padding)是两种常见的数据交换格式,尤其在Web开发中广泛使用。本文将深入探讨这两种格式的特点、用途以及它们在跨域数据请求中的作用。 **1. JSON简介** ...
在本示例中,我们将深入探讨JSONP的工作原理,以及如何使用Python来实现JSONP服务。 **JSONP的原理** JSONP的核心思想是通过动态插入`<script>`标签,其`src`属性指向提供数据的服务端URL。服务端返回的不是标准的...
JSONP简单调用实例。ASP.NET和纯HTML。jQuery的$.ajax的调用!jsonP说白了,就是在json字符串外面包上一个:参数名称+左右括弧!只是包了个:jsonpCallback() 而已! 相关文章:...
JSONP(JSON with Padding)是一种跨域数据交互协议,它利用了`<script>`标签可以跨域请求资源的特性,为了解决JavaScript在浏览器端由于同源策略限制无法直接访问不同源的HTTP请求的问题。在本场景中,前端使用...
### JSONP原理详解 #### 一、什么是JSONP? JSONP(JSON with Padding)是一种用于解决浏览器同源策略限制的方法,允许网页从不同域名的服务器上加载数据。它利用了`<script>`标签不受同源策略限制的特点来实现...
在这个纯Servlet实现JSONP的例子中,我们将深入理解JSONP的工作原理,以及如何在Java的Servlet环境中实现这一功能。 首先,让我们了解JSONP的基本概念。JSON(JavaScript Object Notation)是一种轻量级的数据交换...
JSONP (JSON with Padding) 是一种跨域数据交互协议,主要应用于JavaScript中,用来解决浏览器的同源策略(SOP)限制。同源策略限制了JavaScript只能与相同协议、相同域名、相同端口的源进行交互,但JSONP巧妙地利用...
解决跨域封装的jsonp
JSONP(JSON with Padding)是一种跨域数据交互协议,它利用了`<script>`标签可以跨域请求资源的特性,将JSON数据包裹在一个JavaScript函数调用中返回,从而实现跨域通信。在.NET 3.5中,Windows Communication ...
**Ajax与Jsonp结合在Jquery中的应用** Ajax(Asynchronous JavaScript and XML)技术允许我们在不刷新整个网页的情况下,与服务器进行数据交互,提升用户体验。而Jsonp(JSON with Padding)是解决浏览器同源策略...
然而,为了实现数据交互,开发者们找到了一种名为JSONP(JSON with Padding)的解决方案。本文将深入讲解JSONP的原理及其在解决跨域问题中的应用。 ### JSONP 原理 JSONP 的全称是“JSON with Padding”,它并非一...
在标题"Jsonp+asp.net"中,我们可以理解为这个项目或教程是关于如何在ASP.NET环境下使用JSONP与前端交互数据的。ASP.NET是微软开发的一种Web应用程序框架,它提供了丰富的功能和工具来构建动态网站、Web应用和服务。...
为了解决这个问题,开发者通常采用JSONP(JSON with Padding)技术。本文将详细探讨JSONP的工作原理以及如何在实际项目中使用它来跨域获取JSON数据。 ### JSONP简介 JSONP全称为"JSON Padding",它并不是JSON的一...
后台php设置jsonp
然而,Ajax技术在实现动态网页交互时常常需要跨越这个限制,这时就引入了JSONP(JSON with Padding)作为跨域请求的一种解决方案。本文将详细介绍JSONP的工作原理以及如何在ASP.NET网站开发中应用JSONP解决Ajax跨域...
- **JSONP**:一种允许从一个域加载另一个域中的脚本的技术,它通过动态创建`<script>`标签并插入到DOM中来实现。 - **跨域限制**:浏览器的安全策略之一,限制了从一个源加载的脚本获取或设置另一个源上的资源的...
JSONP(JSON with Padding)是一种跨域数据交互协议,它的基本思想是利用JavaScript函数调用的特性,将数据封装在函数调用中,从而绕过浏览器的同源策略限制。在网页开发中,如果你需要从一个不同的域名下获取数据,...
JSONP(JSON with Padding)是一种跨域数据交互协议,它利用了HTML中`<script>`标签可以跨域请求...随着CORS(跨源资源共享)的普及,JSONP的重要性逐渐被CORS取代,但理解JSONP的工作原理对于前端开发者仍然很重要。