回调函数调用后需要将添加的script标签进行删除,并将函数置空
function myJSONP(url, callback) {
let id = Symbol()
window[id] = function(result) {
if(callback) {
callback(result)
}
let getId = document.getElementById('id')
getId.parentNode.removeChild('id')
window[id] = null
}
url = url.replace('callback=?', 'callback='+id)
let script = document.createElement('script')
script.setAttribute('id', id)
script.setAttribute('src', url)
script.setAttribute('type', text/javascript)
document.body.appendChild(script)
}
网友评论