[Pkg-javascript-commits] [sockjs-client] 06/434: Opera and IE jsonp script quirks.

Tonnerre Lombard tonnerre-guest at moszumanska.debian.org
Wed Jan 8 00:46:57 UTC 2014


This is an automated email from the git hooks/post-receive script.

tonnerre-guest pushed a commit to branch master
in repository sockjs-client.

commit 6a1b16be96b980a0958e884db74726a938e30ed3
Author: Marek Majkowski <majek04 at gmail.com>
Date:   Sat Jul 23 00:04:18 2011 +0100

    Opera and IE jsonp script quirks.
    
    Based on jQuery-jsonp code:
    http://jaubourg.net/2010/07/loading-script-as-onclick-handler-of.html
    https://code.google.com/p/jquery-jsonp/source/browse/trunk/core/jquery.jsonp.js
---
 COPYING            |  6 +++--
 lib/trans-jsonp.js | 65 ++++++++++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 60 insertions(+), 11 deletions(-)

diff --git a/COPYING b/COPYING
index f1f7f76..502617b 100644
--- a/COPYING
+++ b/COPYING
@@ -1,9 +1,11 @@
 Parts of the code are derived from various open source projects.
 
 For code derived from Socket.IO by Guillermo Rauch see
-LICENSE-MIT-Socket.io, most notably:
-  o lib/trans-jsonp.js
+LICENSE-MIT-Socket.io, most notably.
 
 Snippets derived from JSON-js by Douglas Crockford are public domain.
 
+Snippets derived from jQuery-JSONP by Julian Aubourg, generic MIT
+license.
+
 All other code is released on MIT license, see LICENSE-MIT-SockJS.
diff --git a/lib/trans-jsonp.js b/lib/trans-jsonp.js
index 255aee8..cdd5e49 100644
--- a/lib/trans-jsonp.js
+++ b/lib/trans-jsonp.js
@@ -95,19 +95,27 @@ var jsonPReceiverWrapper = function(url, constructReceiver, user_callback) {
     return stop;
 };
 
+// Parts derived from Socket.io:
+//    https://github.com/LearnBoost/socket.io/blob/0.6.17/lib/socket.io/transports/jsonp-polling.js
+// and jQuery-JSONP:
+//    https://code.google.com/p/jquery-jsonp/source/browse/trunk/core/jquery.jsonp.js
 var jsonPGenericReceiver = function(url, callback) {
     var script = document.createElement('script');
+    var script2;
     var close_script = function(v, t) {
+        if (typeof script2 !== 'undefined') {
+            script2.parentNode.removeChild(script2);
+            script2 = undefined;
+        }
         if (typeof script !== 'undefined') {
             callback(v, t);
             script.parentNode.removeChild(script);
-            script.onreadystatechange = script.onerror = script.onload = null;
+            script.onreadystatechange = script.onerror = script.onload = script.onclick = null;
             delete script;
             script = callback = undefined;
         }
     };
-    script.async = true;
-    script.defer = true;
+    script.id = 'a' + utils.random_string(8);
     script.src = url;
     script.type = 'text/javascript';
     script.charset = 'UTF-8';
@@ -120,18 +128,56 @@ var jsonPGenericReceiver = function(url, callback) {
                      'close');
     };
     script.onreadystatechange = function(e) {
-        if (script.readyState == 'loaded' ||
-            script.readyState == 'complete') {
-            close_script({status:1001, reason:"Onreadystatechange triggered on script"},
-                         'close');
+        if (script.readyState == 'loaded' || script.readyState == 'complete') {
+            if (typeof script !== 'undefined' && script.htmlFor && script.onclick) {
+                try {
+                    script.onclick();
+                } catch (x) {}
+            }
+            if (typeof script !== 'undefined') {
+                close_script({status:1001, reason:"Onreadystatechange triggered on script"},
+                             'close');
+            }
         }
     };
+    // IE: event/htmlFor/onclick trick.
+    // One can't rely on proper order for onreadystatechange. In order to
+    // make sure, set a 'htmlFor' and 'event' properties, so that
+    // script code will be installed as 'onclick' handler for the
+    // script object. Later, onreadystatechange, manually execute this
+    // code. FF and Chrome doesn't work with 'event' and 'htmlFor'
+    // set. For reference see:
+    // http://jaubourg.net/2010/07/loading-script-as-onclick-handler-of.html
+    if (typeof script.async === 'undefined') {
+        // According to mozilla docs, in recent browsers script.async defaults
+        // to 'true', so we may use it to detect a good browser:
+        // https://developer.mozilla.org/en/HTML/Element/script
+        if (typeof document.attachEvent === 'object') {
+            // ie
+            try {
+                script.htmlFor = script.id;
+                script.event = "onclick";
+            } catch (x) {}
+            script.async = true;
+            script.defer = true;
+        } else {
+            // opera
+            script2 = document.createElement('script');
+            script2.text = "try{document.getElementById('"+script.id+"').onerror();}catch(x){};";
+        }
+    } else {
+        script.async = true;
+        script.defer = true;
+    }
+
     var head = document.getElementsByTagName('head')[0];
     head.insertBefore(script, head.firstChild);
+    if (script2) {
+        head.insertBefore(script2, head.firstChild);
+    }
     return close_script;
 };
 
-
 var jsonPGenericSender = function(url, messages, callback) {
     var that = this;
     if (!('_send_form' in that)) {
@@ -142,6 +188,7 @@ var jsonPGenericSender = function(url, messages, callback) {
         form.style.position = 'absolute';
         form.method = 'POST';
         form.enctype = 'application/x-www-form-urlencoded';
+        form.acceptCharset = "UTF-8";
         form.appendChild(area);
         document.body.appendChild(form);
     }
@@ -177,4 +224,4 @@ var jsonPGenericSender = function(url, messages, callback) {
         if (iframe.readyState == 'complete') completed();
     };
     return completed;
-};
\ No newline at end of file
+};

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/sockjs-client.git



More information about the Pkg-javascript-commits mailing list