gpt4 book ai didi

javascript - 是什么导致 While (1) 循环删除网站的代码?

转载 作者:行者123 更新时间:2023-11-28 12:55:27 25 4
gpt4 key购买 nike

我在 JavaScript 代码中运行了一些 while 循环,并且很好奇是什么导致循环从网站中删除了所有代码(除了单个 header )。我认为这是因为循环是无限的(因为 1 总是等于 1),但很好奇为什么它不只是让网站崩溃。是不是因为document.write没有指定到某个路径?

javascript: (function() { while (1) {  document.write('Y'); }})();

编辑:感谢您的帮助,似乎 document.open 已运行并导致页面代码被删除。然后浏览器认为这是由于实际网站的代码造成的,Chrome 发出了一条错误消息。

最佳答案

document.write()是一个DOM阻塞函数,也就是说,浏览器会中断解析。因此,当您的函数被调用时,浏览器将永远停留在该函数上,并且无法继续正确渲染网站。

另一个效果如下:

as document.write writes to the document stream, calling document.write on a closed (loaded) document automatically calls document.open, which will clear the document.

https://developer.mozilla.org/en-US/docs/Web/API/Document/write

两者都取决于时间并且可能涉及竞争条件。

请阅读此问答以获取有关 SO 的更多信息:

Why is document.write considered a "bad practice"?

关于javascript - 是什么导致 While (1) 循环删除网站的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56006758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com