|
23 | 23 | return 'jsonp_' + Date.now() + '_' + Math.ceil(Math.random() * 100000); |
24 | 24 | } |
25 | 25 |
|
26 | | - // Known issue: Will throw 'Uncaught ReferenceError: callback_*** is not defined' |
27 | | - // error if request timeout |
28 | 26 | function clearFunction(functionName) { |
29 | 27 | // IE8 throws an exception when you try to delete a property on window |
30 | 28 | // http://stackoverflow.com/a/1824228/751089 |
|
37 | 35 |
|
38 | 36 | function removeScript(scriptId) { |
39 | 37 | var script = document.getElementById(scriptId); |
40 | | - document.getElementsByTagName('head')[0].removeChild(script); |
| 38 | + if (script) { |
| 39 | + document.getElementsByTagName('head')[0].removeChild(script); |
| 40 | + } |
41 | 41 | } |
42 | 42 |
|
43 | 43 | function fetchJsonp(_url) { |
|
79 | 79 | jsonpScript.setAttribute('charset', options.charset); |
80 | 80 | } |
81 | 81 | jsonpScript.id = scriptId; |
82 | | - jsonpScript.onerror = function () { |
83 | | - reject(new Error('JSONP request to ' + _url + ' failed')); |
84 | | - |
85 | | - clearFunction(callbackFunction); |
86 | | - removeScript(scriptId); |
87 | | - }; |
88 | 82 | document.getElementsByTagName('head')[0].appendChild(jsonpScript); |
89 | 83 |
|
90 | 84 | timeoutId = setTimeout(function () { |
|
93 | 87 | clearFunction(callbackFunction); |
94 | 88 | removeScript(scriptId); |
95 | 89 | }, timeout); |
| 90 | + |
| 91 | + // Caught if got 404/500 |
| 92 | + jsonpScript.onerror = function () { |
| 93 | + reject(new Error('JSONP request to ' + _url + ' failed')); |
| 94 | + |
| 95 | + clearFunction(callbackFunction); |
| 96 | + removeScript(scriptId); |
| 97 | + if (timeoutId) clearTimeout(timeoutId); |
| 98 | + }; |
96 | 99 | }); |
97 | 100 | } |
98 | 101 |
|
|
0 commit comments