tags:

views:

111

answers:

5

I have partial control of a web page where by I can enter snippets of code at various places, but I cannot remove any preexisting code.

There is a script reference midway through the page

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

but I do not want the script to load. I cannot access the unwanted.js file. Is there anyway I can use javascript executing above this refernce to cause the unwanted.js file not to load?

Edit: To answer the comments asking what and why:

I'm setting up a Stack Exchange site and the WMD* js file loads halfway down the page. SE will allow you to insert HTML in various parts of the page - so you can have your custom header and footer etc. I want to override the standard WMD code with my own version of it. I can get around the problem by just loading javascript after the original WMD script loads and replacing the functions with my own - but it would be nice not to have such a large chunk of JS load needlessly.

*WMD = the mark down editor used here at SO, and on the SE sites.

+1  A: 

In short, you can't. Even if there is a hack, it would heavily depend on the way browsers parse the HTML and load the scripts and hence wouldn't be compatible with all browsers.

Chetan Sastry
+3  A: 

Please tell us exactly what you can and cannot do, and (preferably; this sounds fascinating) why.

If you can, try inserting <!-- before the script include and --> afterwards to comment it out.

Alternatively, look through the script file and see if there's any way that you could break it or nullify its effects. (this would depend entirely on the script itself; if you want more specific advice, please post more details, or preferably, the script itself.

SLaks
Unfortunately there is necessary markup in-between that would get commented out also.
Mr. Flibble
A: 

Could you start an HTML comment above it and end below it in another block?

What does the contents of unwanted.js look like?

Dark Falcon
No can do - see comment to SLaks answer.
Mr. Flibble
So repeat the necessary markup in the block you control.
Dark Falcon
True. The necessary markup id dynamically created on the server so it would be quite difficult but I guess I could parse out the parts of the dom and recreate them.
Mr. Flibble
A: 

You can remove a script from the DOM after it is called by using something simple such as:

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

This will stop all functions of the script but will not take it out of user's cache. However like you wanted it can't be used.

Dreamcube
This won't prevent the script from executing, which is (presumably) what he's trying to do.
SLaks
Correct, but the functions can't be accessed. I guess it may be a misunderstanding on what he actually wants. The script to load at all or just not allowing it to function. The only thing I can think of that makes sense in this whole problem is they get a txteditor to insert in certain areas and he only gets areas above and below where the script is called. Your guess is as good as mine for this one.
Dreamcube
A: 

Basically you can't unless you have access to the page content before you render it. If you can manipulate the HTML before you send it off to the browser, you can write a regular expression that will match the desired piece of code, and remove it.

Mircea Grelus