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

2
  • Does that <script> exist in your actual html? or just a variable? Commented May 6, 2015 at 9:38
  • 3
    Better solution: $('#right-bloc').find('script[src="http://firstScript.com"]').remove(). Commented May 6, 2015 at 9:45

7 Answers 7

3

You can specify it as the following code:

html.find('script[src="http://firstScript.com"]').remove();
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

It's possible to add after remove this script? Because I need to refresh the script
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();
});

Comments

0

The only need changes

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

Comments

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;

1 Comment

To add again your script (if you want to refresh it) you can do this way : $('.right-bloc-pub-1').append('<script type="text/javascript" src="http://firstScript.com"></script>') I recommend you to use the id attribute here rather than the class, assuming it will be unique.
0
$(document).ready(function(){
      $('.right-bloc-pub-1').html().remove();
});

try this

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.