简体   繁体   中英

How do I export mongodb html code to html file?

How do I display an html data piece from mongo db correctly?

Data: The html was encoded

What it shows: Not showing correctly

Code:

 <script defer> function decodeHtml(html) { var txt = document.createElement("textarea"); txt.innerHTML = html; return txt.value; } const description = document.getElementById("descontainer") description.innerHTML += decodeHtml('<%= post.post.description %>') </script>

Not really sure why you put that html on a textarea element. But if the content is comming from a database that is contain use generated content, then you probele need an html sanitizer like dompurify .

You will use it like this:


<script src="https://cdnjs.cloudflare.com/ajax/libs/dompurify/2.3.6/purify.min.js" integrity="sha512-DJjvM/U3zCRpzrYboJgg23iLHapWcS2rlo7Ni18Cdv+FMs6b3gUF7hQihztj4uVkHHfUwk7dha97jVzRqUJ7hg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<script defer>
   function decodeHtml(html) {
            
      return DOMPurify.sanitize(html);;
   }
          
   const description = document.getElementById("descontainer")

   description.innerHTML += decodeHtml('<%= post.post.description %>')
</script>

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