{"ast":null,"code":"'use strict';\n\nvar random = require('../../utils/random'),\n    urlUtils = require('../../utils/url');\n\nvar debug = function () {};\n\nif (process.env.NODE_ENV !== 'production') {\n  debug = require('debug')('sockjs-client:sender:jsonp');\n}\n\nvar form, area;\n\nfunction createIframe(id) {\n  debug('createIframe', id);\n\n  try {\n    // ie6 dynamic iframes with target=\"\" support (thanks Chris Lambacher)\n    return global.document.createElement('<iframe name=\"' + id + '\">');\n  } catch (x) {\n    var iframe = global.document.createElement('iframe');\n    iframe.name = id;\n    return iframe;\n  }\n}\n\nfunction createForm() {\n  debug('createForm');\n  form = global.document.createElement('form');\n  form.style.display = 'none';\n  form.style.position = 'absolute';\n  form.method = 'POST';\n  form.enctype = 'application/x-www-form-urlencoded';\n  form.acceptCharset = 'UTF-8';\n  area = global.document.createElement('textarea');\n  area.name = 'd';\n  form.appendChild(area);\n  global.document.body.appendChild(form);\n}\n\nmodule.exports = function (url, payload, callback) {\n  debug(url, payload);\n\n  if (!form) {\n    createForm();\n  }\n\n  var id = 'a' + random.string(8);\n  form.target = id;\n  form.action = urlUtils.addQuery(urlUtils.addPath(url, '/jsonp_send'), 'i=' + id);\n  var iframe = createIframe(id);\n  iframe.id = id;\n  iframe.style.display = 'none';\n  form.appendChild(iframe);\n\n  try {\n    area.value = payload;\n  } catch (e) {// seriously broken browsers get here\n  }\n\n  form.submit();\n\n  var completed = function (err) {\n    debug('completed', id, err);\n\n    if (!iframe.onerror) {\n      return;\n    }\n\n    iframe.onreadystatechange = iframe.onerror = iframe.onload = null; // Opera mini doesn't like if we GC iframe\n    // immediately, thus this timeout.\n\n    setTimeout(function () {\n      debug('cleaning up', id);\n      iframe.parentNode.removeChild(iframe);\n      iframe = null;\n    }, 500);\n    area.value = ''; // It is not possible to detect if the iframe succeeded or\n    // failed to submit our form.\n\n    callback(err);\n  };\n\n  iframe.onerror = function () {\n    debug('onerror', id);\n    completed();\n  };\n\n  iframe.onload = function () {\n    debug('onload', id);\n    completed();\n  };\n\n  iframe.onreadystatechange = function (e) {\n    debug('onreadystatechange', id, iframe.readyState, e);\n\n    if (iframe.readyState === 'complete') {\n      completed();\n    }\n  };\n\n  return function () {\n    debug('aborted', id);\n    completed(new Error('Aborted'));\n  };\n};","map":null,"metadata":{},"sourceType":"script"}