0

Can anyone tell me why special here?

<html>
    <head>
        <script src="editor.js"></script>
    </head>
    <body>
        <div id="scripts" class="scripts">
            Editor.Execute('<html>Html String</html>');
            Editor.Execute('<something>Html String</something>');
        </div>
    </body>
</html>

document.getElementById("scripts").innerHTML shows something however html dissapears.

    Execute('Html String');
    Execute('<something>Html String</something>');

It behaves the same way in Firefox and Chrome.

Nishant
  • 20,354
  • 18
  • 69
  • 101

2 Answers2

4

You're running into this issue.

Basically, the browser sanitizes out the HTML tags before your JavaScript can even access the page – you can check in the Chrome elements inspector, your <html> tag is not there.

I guess the answer depends on what exactly you're trying to do, but if you're just trying to output that code onto a web page, you can just escape the characters:

<html>
    <body>
        <div id="scripts" class="scripts">
            Execute('&lt;html&gt;Html String&lt;/html&gt;');
            Execute('&lt;something&gt;Html String&lt;/something&gt;');
        </div>
    </body>
</html>

Then document.getElementById('scripts').innerHTML will output:

Execute('&lt;html&gt;Html String&lt;/html&gt;');
Execute('&lt;something&gt;Html String&lt;/something&gt;');

And then you can replace the HTML entities in JavaScript.

Community
  • 1
  • 1
vinsanity555
  • 241
  • 1
  • 11
  • Yes I am aware of escaping.However, I was just curious to see why was behaving differently. also behaves the same way. – Nishant Dec 21 '15 at 20:45
0

Without knowing what you do in that Execute() it is hard to say what is going on there.

Just in case: HTML document can have one and only one <html> node.

c-smile
  • 26,734
  • 7
  • 59
  • 86
  • I have modified the code. Its for a HTML Editor that takes pure HTML as input. So I am just executing it after the page loads. I think the HTML goes into another Iframe. – Nishant Dec 21 '15 at 20:43