Toggle navigation
Home
Latest pastes
FAQ
Random
BitBin is shutting down!
Register
Login
Pure Javascript function for JSONP requests
SUBMITTED BY:
Guest
DATE:
Nov. 5, 2012, 10:10 a.m.
FORMAT:
JavaScript
SIZE:
636 Bytes
Raw
Download
Tweet
HITS:
2862
Go to comments
Report
function
execJSONP
(
url
,
cb
)
{
//url (without "callback=" parameter!) and callback function
var
script
=
document
.
createElement
(
'script'
);
script
.
async
=
true
;
var
callb
=
'exec'
+
Math
.
floor
((
Math
.
random
()
*
65535
)
+
1
);
window
[
callb
]
=
function
(
data
)
{
var
scr
=
document
.
getElementById
(
callb
);
scr
.
parentNode
.
removeChild
(
scr
);
cb
(
data
);
window
[
callb
]
=
null
;
delete
window
[
callb
];
}
var
sepchar
=
(
url
.
indexOf
(
'?'
)
>
-
1
)
?
'&'
:
'?'
;
script
.
src
=
url
+
sepchar
+
'callback='
+
callb
;
script
.
id
=
callb
;
document
.
getElementsByTagName
(
'head'
)[
0
].
appendChild
(
script
);
}
Please enable JavaScript to view the
comments powered by Disqus.
comments powered by
Disqus