简体   繁体   English

如何在C#中的Web浏览器控件中禁用特定的javascript文件

[英]How to disable a specific javascript file in Web Browser control in C#

I'm using web browser control in my C# project and I want to disable only a specific javascript file. 我在C#项目中使用网络浏览器控件,并且只想禁用特定的javascript文件。 Is there any way to do that? 有什么办法吗? This script is loading from an external website and I want to disable it. 该脚本是从外部网站加载的,我想禁用它。 tx 发射

There's a great article on this at javascriptKit.com javascriptKit.com上有一篇很棒的文章

Dynamically removing an external JavaScript or CSS file: 动态删除外部JavaScript或CSS文件:

To remove an external JavaScript or CSS file from a page, the key is to hunt them down first by traversing the DOM, then call DOM's removeChild() method to do the hit job. 要从页面中删除外部JavaScript或CSS文件,关键是首先通过遍历DOM来查找它们,然后调用DOM的removeChild()方法来完成点击工作。 A generic approach is to identify an external file to remove based on its file name, though there are certainly other approaches, such as by CSS class name. 通用方法是根据外部文件的文件名来标识要删除的外部文件,尽管当然还有其他方法,例如通过CSS类名。 With that in mind, the below function removes any external JavaScript or CSS file based on the file name entered: 考虑到这一点,下面的函数根据输入的文件名删除所有外部JavaScript或CSS文件:

 function removejscssfile(filename, filetype){
 var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist from
 var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
 var allsuspects=document.getElementsByTagName(targetelement)
 for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
  if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(filename)!=-1)
   allsuspects[i].parentNode.removeChild(allsuspects[i]) //remove element by calling parentNode.removeChild()
 }
}

removejscssfile("somescript.js", "js") //remove all occurences of "somescript.js" on page
removejscssfile("somestyle.css", "css") //remove all occurences "somestyle.css" on page

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM