简体   繁体   中英

Javascript Return HTML and css content as a string but not the javascript

is there a way of getting all the content of the page HTML , CSS , but exclude all the java script functions and script src?

        var htmlPage = $("html").html();
    console.log(htmlPage);

I know that will give me all of it. but I need to exclude the JS from the results

EDIT: fixed the regex (non-greedy version)

You can try this:

var htmlPage = $("html").html().replace(/<script[\s\S]*?<\/script>/mig, "");

The regular expression should match all <script> ... </script> tags and replace them with nothing.


BTW this is kind of a lucky shot because the regex itself requires the ending </script> to be escaped with a \\ backslash like this: <\\/script> .

This escape character is why the regex doesn't match itself, which would cause it to fail. So, it works because by escaping it correctly it isn't self-similar anymore.

Another option is to use Element.innerHTML and include the content that you want to get. For example:

<!doctype html>
<html>
  <head>
   <!--Css links goes here--> 
  </head>
  <body>
   <!--Your content-->
    <p>Hello World</p> 
  </body>
 <script>
 //Js
 </script>
<html>
var body = document.body.innerHTML;
var head = document.head.innerHTML;

Then you can concadenate or whatever you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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