JavaScript: Using document.write();

JavaScript: Using document.write();

本文关键字:write document Using JavaScript      更新时间:2023-09-26

我试图在JavaScript中使用document.write INSIDE另一个document.write创建一个新的HTML文档。

我知道这是一种痛苦的方法(并且可以随意重写代码以获得所需的输出),但是这里是:

var newDoc = document.open("text/html", "replace");
                newDoc.write("<html><head><title>Failure!</title></head><body><h5>js::error 5015 &&syn.0</h5><p>In Quantum Mechanisms exists a quantum state known as quantum superposition - the state of being two things at once given a random subatomic event that may or may not occur.</p><p>A famous experiment by Erwin Schrödinger, a cat was placed in a box along with a poison flask that would kill the cat, only activating if a subatomic change was detected (which is completely random). The thought experiment brought forth the idea of the cat being <em>dead</em> and <em>alive</em> at the same exact time, but you aren't able to know which it is. So in theory, by opening the box and finding that the cat is dead, you have essentially killed the cat yourself.</p><p>Quantum Mechanics have been applied to the button you just clicked. The choice was yours on whether or not to click it, and you did not know where you would end up. You can say that it would have unlocked a new upgrade, or break the game, but since you clicked the button, you therefore must have broken the game yourself. Just like that, curiosity killed the cat. Sorry, quantum superposition is pretty terrible. Fortunately, you can click this button below to continue on your journey.</p><button onclick='"var newDoc = document.open('"text/html'", '"replace'"); newDoc.write('"<html><head><title>Double Fail</title></head><body><p>No seriously, quantum mechanics are pretty bad. :)</p></body></html>'"); newDoc.close();'">Continue</button></body></html>");
                newDoc.close();

如您所见,按钮没有清楚地输出信息。我不知道我做错了什么

需要进行大量转义:

var newDoc = document.open("text/html", "replace");
                newDoc.write('<html><head><title>Failure!</title></head><body><h5>js::error 5015 &&syn.0</h5><p>In Quantum Mechanisms exists a quantum state known as quantum superposition - the state of being two things at once given a random subatomic event that may or may not occur.</p><p>A famous experiment by Erwin Schrödinger, a cat was placed in a box along with a poison flask that would kill the cat, only activating if a subatomic change was detected (which is completely random). The thought experiment brought forth the idea of the cat being <em>dead</em> and <em>alive</em> at the same exact time, but you aren''t able to know which it is. So in theory, by opening the box and finding that the cat is dead, you have essentially killed the cat yourself.</p><p>Quantum Mechanics have been applied to the button you just clicked. The choice was yours on whether or not to click it, and you did not know where you would end up. You can say that it would have unlocked a new upgrade, or break the game, but since you clicked the button, you therefore must have broken the game yourself. Just like that, curiosity killed the cat. Sorry, quantum superposition is pretty terrible. Fortunately, you can click this button below to continue on your journey.</p><button onclick="var newDoc = document.open(''text/html'', ''replace''); newDoc.write(''<html><head><title>Double Fail</title></head><body><p>No seriously, quantum mechanics are pretty bad. :)</p></body></html>''); newDoc.close();">Continue</button></body></html>');
                newDoc.close();

问题开始了:

</p><button onclick='"var newDoc = document.open('"text/html'"
                     ^^ starting onclick         ^^ ending it

var newDoc = document.open("text/html", "replace");
newDoc.write("<html><head><title>Failure!</title></head><body>Button:<button onclick='"var newDoc = document.open(&quot;text/html&quot;, &quot;replace&quot;); newDoc.write(&quot;<html><head><title>Double Fail</title></head><body><p>Button clicked.</p></body></html>&quot;); newDoc.close();'">Continue</button></body></html>");
newDoc.close();

你的报价很差。生成的HTML将在"上中断,您需要在onclick属性中使用HTML转义(&quot;),而不是JavaScript转义('")。