Remove a script with jQuery

I have a problem with my jQuery. So, I want to remove a script who is inside a <div>. My html is :

<div id="right-bloc">
        <div class="right-bloc-pub-1">
            <script type="text/javascript" src="http://firstScript.com"></script>
        </div>
        <div class="right-bloc-pub-2">
            <script type="text/javascript" src="http://secondScript.com"></script>
        </div>
</div>

My jQuery :

 var stringOfHtml = '<script type="text/javascript" src="http://firstScript.com"></script>';
 var html = $(stringOfHtml);
 html.find('script').remove();

I want to remove the script witch contains http://firsttScript.com, but not working. It's possible to remove this script and add after? Because I need to refresh this script

Answers:

Answer

You can specify it as the following code:

html.find('script[src="http://firstScript.com"]').remove();
Answer

Assuming the <script> tags are actually in your html DOM and you have jQuery reference included.

You can use .filter() to get the script with src as 'http://firstScript.com' and the .remove().

$('html').find('script').filter(function(){
    return $(this).attr('src') === 'http://firstScript.com'
}).remove();

Also,

var stringOfHtml = '<script type="text/javascript" src="http://firstScript.com"></script>';
var html = $(stringOfHtml);

This is Not allowed and would throw error Unexpected Token Illegal

Answer

Try This:

   $(document).ready(function(){
     $('.right-bloc-pub-1').find('script[src="http://firstScript.com"]').remove();
   });

OR

$(document).ready(function(){
   $('.right-bloc-pub-1').find('script').attr('src', 'http://firstScript.com').remove();
});

FIDDLE

Answer

The only need changes

    var html = $(stringOfHtml);
    html.remove();
Answer

To select and remove your first script you can do this way : $('script[src="http://firstScript.com"]').remove()

Just know that this will remove the script from your DOM but not the library loaded within this script. For example if your first script load jQuery, the $ variable will still contains jQuery. To remove completely jQuery you can add $ = undefined; jQuery = undefined;

Answer
$(document).ready(function(){
      $('.right-bloc-pub-1').html().remove();
});

try this

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.