简体   繁体   English

JavaScript:使用document.write();

[英]JavaScript: Using document.write();

I'm attempting to create a new HTML doc using document.write INSIDE another document.write in JavaScript. 我正在尝试使用document.write创建新的HTML document.write在另一个document.write中使用JavaScript。

I know this is a painful way to do it (and feel free to rewrite the code with the desired output), but here it is: 我知道这是一种痛苦的方式(可以随意用所需的输出重写代码),但是这里是:

 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(); 

As you can see, the button is not outputting the information clearly. 如您所见,该按钮没有清楚地输出信息。 Not sure what i'm doing wrong. 不知道我在做什么错。

A lot of escaping needs to happen: 发生许多逃避需求:

 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(); 

Here's where the problem starts: 这是问题开始的地方:

</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(); 

Your quoting is bad. 您的报价不好。 The generated HTML will break on " , you need to employ HTML escaping ( &quot; ) inside your onclick attribute, not JavaScript escaping ( \\" ) at that point. 生成的HTML将以"结尾,您需要在onclick属性中采用HTML换码( &quot; ),而不是JavaScript换码( \\" )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM