开发者

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

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.j开发者_StackOverflow社区s 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.


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.


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.


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

What does the contents of unwanted.js look like?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜