1

I need to remove two <script> tags from a page dynamically, at the document ready. Every tag has its own id; I've tried to insert in the code:

$("#idPrimoScript").remove();
$("#idSecondoScript").remove();

but nothing happens...

Some ideas? Thanks

8
  • you can also include script after html body code. Commented Sep 10, 2013 at 10:20
  • Please show us the relevant html as well. If the above code does not work (when run at document.ready as shown below) then there is something else causing the problem. Commented Sep 10, 2013 at 10:21
  • Can you post your your html code too? Commented Sep 10, 2013 at 10:21
  • 4
    I may be wrong, but I don't think removing the tags will remove the script, since it's already loaded. Commented Sep 10, 2013 at 10:26
  • 1
    Thanks @A.Wolff. I've learnt something new so I can go back to bed now until tomorrow :D Commented Sep 10, 2013 at 10:32

2 Answers 2

1

As I can understand from your question, you want to remove (actually disable) the scripts included in your 2 script tags. a work around I usually used to make in these situations is to introduce a variable which will act as a flag or a passing signal. let's say that I will create a global variable which will be the flage I will be using:

var enableMySpecialScript = true

as you can see, I initially set the value of this flag to true, and whenever I need to disable the special script inside my page, I set this flag to false, then inside my script file, i always check for the flag's value,so when it's only "true", the script will execute, meaning that when it's false the script will not run, which is exactly what you are asking for.

This way you don't have to mess up with removing stuff or altering functions, and from my experience with this solution, it's a clean and debuggable one :)

Sign up to request clarification or add additional context in comments.

3 Comments

This is very good solution, but the file that I call is an external js, form another site. <script type="text/javascript" src="example.com/myScript.js"> </script>
what is the behavior of this script? is it something that hooks itself at the beginning? or something that is triggered when some event is being fired?
no, it starts every page load, and is contained in the footer.php, along with another script. In practice, I can not access the source of that file. The script check the visit on a site, similar Google Analytics.
0

Apparently (having not tried this myself beyond a simple proof script) the way to do this is to create another function with the same name.

See:

How to override a function in another javascript file?

JavaScript: Overriding alert()

Override jQuery functions

Overriding a function in jQuery plugin

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.