From 60794bcf883ebe3689975d358d98365b47465a9c Mon Sep 17 00:00:00 2001 From: rwldrn Date: Wed, 16 Feb 2011 16:47:47 -0500 Subject: [PATCH] [#347] Fixes JSONP contexts --- popcorn.js | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/popcorn.js b/popcorn.js index ec2b790..846fad5 100644 --- a/popcorn.js +++ b/popcorn.js @@ -987,32 +987,48 @@ paramStr = url.split("?")[1], fired = false, params = [], - callback; + callback, parts, callparam; if ( paramStr && !isScript ) { params = paramStr.split("&"); } + + if ( params.length ) { + parts = params[ params.length - 1 ].split("="); + } + + callback = params.length ? ( parts[1] ? parts[1] : parts[0] ) : "jsonp"; - callback = params.length ? params[ params.length - 1 ].split("=")[1] : "jsonp"; if ( !paramStr && !isScript ) { url += "?callback=" + callback; } - script.src = url; - if ( callback && !isScript ) { - // define the jsonp success callback globally + + // If a callback name already exists... + if ( !!window[ callback ] ) { + // Create a unique new callback name + callback = Popcorn.guid( callback ); + } + + // Define the jsonp success callback globally window[ callback ] = function ( data ) { success && success( data ); fired = true; }; + + // Replace callback param and callback name + url = url.replace( parts.join("="), parts[0] + "=" + callback ); + } - + + script.src = url; + script.onload = script.onreadystatechange = function() { // Executing remote scripts -- 2.43.0