Permalink
Browse files

experiments with removing jsonp script

  • Loading branch information...
1 parent 65593e3 commit 0b5003eb59483c70f66e68ae091911d8a8c14465 @robertpalmer robertpalmer committed Feb 24, 2011
Showing with 35 additions and 17 deletions.
  1. +35 −17 lib/linccer.js
View
52 lib/linccer.js
@@ -6,6 +6,8 @@ var Linccer = function(_options) {
var isReady;
var timerId;
+ var connections;
+
var options = { 'server' : '' };
HC.extend(options, _options);
@@ -34,30 +36,46 @@ var Linccer = function(_options) {
var jsonpName = 'jsonp' + Math.floor(Math.random() * 10000000001);
var scriptURL = "https://linccer" + options["server"] + ".hoccer.com/v3" + scriptName + "&jsonp=" + jsonpName;
// var scriptURL = "http://127.0.0.1:9292/v3" + scriptName + "&jsonp=" + jsonpName;
-
- var script = document.createElement("script");
+ var head = document.getElementsByTagName("head")[0] || document.documentElement,
+ script = document.createElement("script");
+
script.setAttribute("type", "text/javascript");
script.setAttribute("src", scriptURL);
-
- document.body.appendChild(script);
+
+ if (script.src.match(/receive.js/)) {
+ console.log('receive with', jsonpName);
+ }
+
+ head.appendChild(script);
- window[jsonpName] = function(data) {
- if (callbackFunctionName !== undefined
- && that[callbackFunctionName] !== undefined) {
+ window[jsonpName] = function(jsonpName, script) {
+ return function(data) {
+ if (head) {
+ if (script.src.match(/receive.js/)) {
+ console.log('release script', script.src);
+ }
+ head.removeChild(script);
+ }
+
+ console.log('undefining', jsonpName);
+ window[jsonpName] = undefined;
+
+ if (callbackFunctionName !== undefined
+ && that[callbackFunctionName] !== undefined) {
that[callbackFunctionName](data);
+ }
}
-
- window[jsonpName] = undefined;
- document.body.removeChild(script);
- delete script;
- }
+ }(jsonpName, script);
if (timeout !== undefined) {
- setTimeout(function() {
- if (window[jsonpName] !== undefined) {
- window[jsonpName]( { message: 'request_timeout' } );
+ setTimeout(function(name) {
+ return function() {
+ if (window[name] !== undefined) {
+ console.log('sending request timeout', name);
+ window[name]( { message: 'request_timeout' } );
+ }
}
- }, timeout);
+ }(jsonpName), timeout);
}
}
@@ -133,7 +151,7 @@ var Linccer = function(_options) {
var timeout = options['timeout'];
delete options['timeout'];
}
-
+
params = [];
params.push('mode=' + mode);

0 comments on commit 0b5003e

Please sign in to comment.