Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an HTML string into an HTML document?

I want to convert my HTML string below in an HTML document. The code below works fine on Firefox and Chrome but not in other browsers (Opera, Safari, IE). Can you help me?

var content = '<iframe width="640" height="360" src="//www.youtube.com/embed/ZnuwB35GYMY" frameborder="0" allowfullscreen></iframe>';

var parser = new DOMParser();
var htmlDoc = parser.parseFromString(content,"text/html");

Thank you not to reply in JQuery.

I want to do exactly that, but in javascript

<?php
    $content = '<iframe width="640" height="360" src="//www.youtube.com/embed/ZnuwB35GYMY" frameborder="0" allowfullscreen></iframe>';
    $doc = new DOMDocument();
    $doc->loadHTML($content);
?>

My main goal is to transform my HTML text in an HTML document in order to change the Width and Height properties of my iframe.

like image 696
Florent Avatar asked Feb 13 '14 14:02

Florent


People also ask

How do I convert text to HTML format?

On the Tools menu, click Options, and then click the Mail Format tab. Under Message Format, in the Compose in this message format list, click HTML or Plain Text, and then click OK.

How do you render a string with HTML tags in react?

To render the html string in react, we can use the dangerouslySetInnerHTML attribute which is a react version of dom innerHTML property. The term dangerously is used here to notify you that it will be vulnerable to cross-site scripting attacks (XSS).

How do I convert HTML text to normal text in Java?

Just call the method html2text with passing the html text and it will return plain text.


1 Answers

Try this

function toNode(iframeString) {
    var div = document.createElement('div');
    div.innerHTML = iframeString;
    iframeNode = div.getElementsByTagName('iframe')[0];
    return iframeNode;
}
like image 143
acehko Avatar answered Oct 08 '22 13:10

acehko