Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove <script> tags from an HTML page using C#?

<html>
    <head>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
            if (window.self === window.top) { $.getScript("Wing.js"); }
        </script>
   </head>
</html>

Is there a way in C# to modify the above HTML file and convert it into this format:

<html>
    <head>
    </head>
</html>

Basically my goal is to remove all the JavaScript from the HTML page. I don't know what is be the best way to modify the HTML files. I want to do it programmatically as there are hundreds of files which need to be modified.

like image 638
StackOverflowVeryHelpful Avatar asked Oct 16 '13 22:10

StackOverflowVeryHelpful


People also ask

How do I remove a script in HTML?

Select the HTML element which need to remove. Use JavaScript remove() and removeChild() method to remove the element from the HTML document.

How do I remove a script?

To remove a script from an applicationClick the Resources folder, right-click the script, and then click Remove.

How do I remove a DOM script tag?

We can remove a script from the DOM by scanning through all scripts on the page, getting the parent node of that script, and then finally removing the child of that parent node.

How do I replace a script tag?

Like the other answers have outlined the only way to replace a script is to locate it, place a new one after it and then remove the original. Save this answer. Show activity on this post. you can set the id for script tag and change the content based on the id instead of replacing it with new one.


1 Answers

It can be done using regex:

Regex rRemScript = new Regex(@"<script[^>]*>[\s\S]*?</script>");
output = rRemScript.Replace(input, "");
like image 153
Jerry Avatar answered Sep 20 '22 11:09

Jerry