Как преобразовать строку в html js
Перейти к содержимому

Как преобразовать строку в html js

  • автор:

Convert String to HTML in JavaScript

Convert String to HTML in JavaScript

In JavaScript, some specific convention makes the overall developing history one step ahead to be integrated. Likewise, one of the non-static ways of addressing a string (in the form of an HTML element donating pattern) and later passing it to the HTML body as a unique piece of object.

This makes the interface dynamic and can solve many issues that would have been tough to call otherwise.

The code examples in the following content will demonstrate how to implement this conversion. Firstly, we will pass the string with the innerHTML property.

In the next example, we will use the DOM parse method. This convention is not encouraged mostly, as it has issues with acceptance to many browsers.

In the final section, we will examine if the string we passed was an HTML object or just strings. Let’s dive in!

Use innerHTML Property to Convert String to HTML Object

Here, we will have a function stringToHTML that will take the raw string as its parameter. After that, we will create a div , and we wish to pass the string given inside that.

We could also pass it to the HTML body instead, but to be neat, we expect a div element.

Next, the newly created div will be associated with an instance dom (supposedly). So, for dom , we will set the innerHTML property and then pass the string.

The return will be the dom instance for the function stringToHTML we created. Let’s check the code lines.

Use DOMParser() Interface to Convert String to HTML Object

The DOMParser() is often ignored or can be used along with conditions. If the prior way of handling the issues gets obliterated, then this segment of code might fire to back up the process.

So here, we will take an instance of the DOMParser() interface, and the instance will be triggered by parseFromString() . The parameters will be the string and the type in HTML it is supposed to represent.

We will then pass the instance doc to the HTML body.

Use jQuery to Ensure the Type of the String Passed in HTML

In this section, we will determine the overall task. We will check if the HTML object was made, the type, etc.

If we can use jQuery to pass a string, it goes to HTML in an object form. Though the content hasn’t been previewed, it has created its space in the HTML body (not permanent).

So, let’s jump to the code block.

Era is an observer who loves cracking the ambiguos barriers. An AI enthusiast to help others with the drive and develop a stronger community.

How to HTML-encode a String

This tutorial provides some methods that are used for HTML-encoding a string without an XSS vulnerability.

Here is an example which somehow reduces the XSS chance:

On the htmlEncode function the innerText of the element is set, and the encoded innerHTML is retrieved. The innerHTML value of the element is set on the htmlDecode function the innerText is retrieved.

In the following html code, we use the functions we have defined to convert a user input in a textarea, and encode it to prevent XSS.

This method will work fine in many scenarios, but in some cases, you will end up with a XSS vulnerability.

For the function above, consider the following string:

The string contains an unescaped HTML tag, so instead of decoding the htmlDecode function will run JavaScript code specified inside the string. To avoid this you can use DOMParser which is supported in all major browsers:

w3docs logoJavascript decoding the HTML

Another useful and fast method exists which also encodes quote marks:

To escape forward-slash / for anti-XSS safety purposes use the following:

The replace() Method

The replace() RegExp method replaces the specified string with another string. The method takes two parameters the first one is the string that should be replaced, and the second one is the string replacing from the first string. The second string can be given an empty string so that the text to be replaced is removed.

converting a javascript string to a html object [duplicate]

pawel's user avatar

You cannot do it with just method, unless you use some javascript framework like jquery which supports it ..

but still, it would not be found by the getElementById because for that to work the element must be in the DOM. just creating in the memory does not insert it in the dom.

You would need to use append or appendTo or after etc.. to put it in the dom first..

Of’course all these can be done through regular javascript but it would take more steps to accomplish the same thing. and the logic is the same in both cases..

Как преобразовать строку в html js

April 26, 2021 — 2 min read

Ezoic

report this ad Advertisement area

To convert an HTML string into real HTML or DOM, you can use the DOMParser Web API using JavaScript. The DOMParser helps us to parse HTML or XML string into real Document or DOM nodes.

For example, let's say you have a HTML string of h1 tag with the text of Hello World! like this,

Now, to convert this string into a real HTML tag we can use the DOMParser web API.

So first, we have to make a parser using the new keyword like this,

After that, we can use the parseFromString() method in the parser object and pass:

  • the raw HTML string as the first argument
  • and the mime type or the type of the document contained in the string as the second argument. In our case, the mime-type value is text/html .

There are other mime types we can use such as:

  • text/xml
  • application/xml
  • application/xhtml+xml
  • image/svg+xml

So it will look like this,

Now the HTML string is converted to an HTML DOM node. You can now use the usual methods and properties available on a DOM node such as appendChild() , classList , etc.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *