I have partial control over a web page where I can enter snippets of code in dif...">

Programmatically remove the link <script src = "/unwanted.js" ../>

I have partial control over a web page where I can enter snippets of code in different places, but I can’t delete any existing code.

There is a script link halfway through the page

<script src="/unwanted.js" type="text/javascript"></script> 

but I do not want to load the script. I cannot access the unwanted.js file. Is there anyway I can use javascript running on this refernce so that the unwanted.js file does not load?

Change To respond to comments by asking what and why:

I am setting up a Stack Exchange site, and the WMD * js file is uploaded to half the page. SE allows you to embed HTML in various parts of the page - so you can have your own custom header and footer, etc. I want to override the standard WMD code with my own version. I can get around this by simply loading javascript after loading the original WMD script and replacing it with my own functions - but it would be nice to not have such a large piece of JS load unnecessarily.

* WMD = down label editor used here in SO and on SE sites.

+4
source share
5 answers

In short, you cannot. Even if there is a hack, it will depend heavily on how browsers parse HTML and load scripts and, therefore, will not be compatible with all browsers.

+2
source

Please tell us what you can and cannot do, and (preferably, this sounds exciting).

If you can, try pasting <!-- before including the script and --> to comment on this.

Alternatively, view the script file and see if there is a way you could break it or hide its consequences. (this will entirely depend on the script itself, if you want more specific recommendations, write more detailed information or, preferably, a script.

+3
source

Could you start the HTML comment above this and end below this in another block?

What does the content of unwanted .js look like?

0
source

You can remove the script from the DOM after calling it using something simple, for example:

 s = document.getElementById ("my_script"); s.parentNode.removeChild(s); 

This will stop all script functions, but will not remove it from the user's cache. However, as you wish, it cannot be used.

0
source

Basically, you cannot if you do not have access to the contents of the page before you render it. If you can manipulate the HTML before sending it to the browser, you can write a regular expression that will match the desired piece of code and delete it.

0
source

All Articles