remove appended script javascript

后端 未结 8 1641
执笔经年
执笔经年 2020-12-29 15:35

how can I remove my appended script because it causes some problems in my app.

This is my code to get my script

var nowDate = new Date().getTime();
v         


        
相关标签:
8条回答
  • 2020-12-29 16:09

    In case of JSONP and your use of jQuery, why not take full advantage of jQuery's JSONP loading facilities, which do the post-clean-up for you? Loading JSONP resource like this, doesn't leave a trace in the DOM:

    $.ajax({ url: 'http://example.com/', dataType: 'jsonp' })
    

    The <script> tag used for loading is removed instantly as soon it's loaded. If the loading is a success. On the other hand, failed requests don't get cleared automatically and you have to do that yourself with something like this:

    $('head script[src*="callback="]').remove();
    

    called at the right time. It removes all pending JSONP <script> tags.

    0 讨论(0)
  • 2020-12-29 16:12

    I would simply check to see if you've already added the script. Adding it and then removing is adds unnecessary complexity. Something like this should work:

    var scriptAdded = false;
    
    if(scriptAdded == false) {
        document.body.appendChild(script);
        scriptAdded = true;
    }
    
    0 讨论(0)
提交回复
热议问题