Skip to content

Instantly share code, notes, and snippets.

@mailtruck
Created November 6, 2016 04:02
Show Gist options
  • Save mailtruck/65acd414a60dda260f9efd741cc60ab5 to your computer and use it in GitHub Desktop.
Save mailtruck/65acd414a60dda260f9efd741cc60ab5 to your computer and use it in GitHub Desktop.
// https://github.com/webmodules/jsonp
function noop(){}function jsonp(e,o,n){function t(){r.parentNode&&r.parentNode.removeChild(r),window[a]=noop,i&&clearTimeout(i)}function c(){window[a]&&t()}"function"==typeof o&&(n=o,o={}),o||(o={});var r,i,u=o.prefix||"__jp",a=o.name||u+count++,p=o.param||"callback",l=null!=o.timeout?o.timeout:6e4,m=encodeURIComponent,d=document.getElementsByTagName("script")[0]||document.head;return l&&(i=setTimeout(function(){t(),n&&n(Error("Timeout"))},l)),window[a]=function(e){console.log("jsonp got",e),t(),n&&n(null,e)},e+=(~e.indexOf("?")?"&":"?")+p+"="+m(a),e=e.replace("?&","?"),console.log('jsonp req "%s"',e),r=document.createElement("script"),r.src=e,d.parentNode.insertBefore(r,d),c}var count=0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment