2

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
TanGio
  • 766
  • 2
  • 12
  • 34

7 Answers7

3

You can specify it as the following code:

html.find('script[src="http://firstScript.com"]').remove();
Radonirina Maminiaina
  • 6,958
  • 4
  • 33
  • 60
2

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

Shaunak D
  • 20,588
  • 10
  • 46
  • 79
1

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();
});
Saiyam Gambhir
  • 536
  • 3
  • 16
0

The only need changes

    var html = $(stringOfHtml);
    html.remove();
Vishwajeet Bose
  • 430
  • 3
  • 12
0

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;

Ganbin
  • 2,004
  • 2
  • 12
  • 19
  • To add again your script (if you want to refresh it) you can do this way : `$('.right-bloc-pub-1').append('')` I recommend you to use the id attribute here rather than the class, assuming it will be unique. – Ganbin May 06 '15 at 10:01
0
$(document).ready(function(){
      $('.right-bloc-pub-1').html().remove();
});

try this

VJP
  • 34
  • 5
0

You need to delete events related with that script. So you must look for events that belong to that script. The $._data(document,'events') is best choice to start with.

AlASAD WAIL
  • 775
  • 4
  • 18