为什么 document.write 不再为我工作?

发布于 2024-07-19 07:00:23 字数 681 浏览 7 评论 0原文

我正在 DotNetNuke 工作,但这似乎并不是严格意义上的 DNN 问题。

我在名为 FormatEmail 的模块中使用 DNN 提供的方法,该方法使用 document.write 写出电子邮件,如下所示:

<script language="text/javascript">

<!--
  document.write(String.fromCharCode(60,97,32,104,114,101,102,61,34,109,97,105,108,116,111,58,119,101,98,109,105,110,64,97,116,101,110,118,101,108,100,116,46,111,114,103,34,62,119,101,98,109,105,110,64,97,116,101,110,118,101,108,100,116,46,111,114,103,60,47,97,62))
// -->

</script>

我刚刚安装了 DNN 5,我知道它包括 jQuery 以及代码库的其他附加内容。 jQuery 会阻止 document.write 代码工作吗?

DNN 是否应该使用另一种方法来隐藏来自机器人的文本?

我应该停止使用此方法来隐藏我的电子邮件地址吗?

更新: 该页面未使用 xhtml。

I'm working in DotNetNuke but this doesn't really seem to be strictly a DNN problem.

I am using a DNN provided method in my module called FormatEmail which uses document.write to write out an email like like so:

<script language="text/javascript">

<!--
  document.write(String.fromCharCode(60,97,32,104,114,101,102,61,34,109,97,105,108,116,111,58,119,101,98,109,105,110,64,97,116,101,110,118,101,108,100,116,46,111,114,103,34,62,119,101,98,109,105,110,64,97,116,101,110,118,101,108,100,116,46,111,114,103,60,47,97,62))
// -->

</script>

I just installed DNN 5 which I know includes jQuery among other additions to the codebase. Will jQuery stop the document.write code from working?

Should DNN be using another method to cloak text from bots?

Should I stop using this method as a way of cloaking my email addresses?

Update:
The page is not using xhtml.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悲歌长辞 2024-07-26 07:00:23

我不知道是否发生了这种情况,但是如果您的网站告诉浏览器它是严格的 XHTML,则 document.writedocument.writeln 将不起作用。 我相信要发生这种情况,您必须使用严格的 DOCTYPE 并将 Content-Type 标头设置为 application/xml+xhtml 而不是 text/html (许多服务器下的默认设置)。 这是因为以这种方式操作 DOM 可能会破坏它。 例如,如果我将以下内容放在经过验证的网页中:

<script type="text/javascript">
<!--
    document.write("</body>");
// -->
</script>

该文档将经过验证并且符合 XHTML,但在大多数浏览器中不起作用。

另一种方法是创建一个 DOM 节点,在其中插入电子邮件地址,并在页面加载时插入它。 例如:

<p>My email address is <span id="email"></span>.</p>
<script type="text/javascript">
<!--
    document.body.onload = function() {
        document.getElementById("email").textContent = String.fromCharCode(60,97,32,104,114,101,102,61,34,109,97,105,108,116,111,58,119,101,98,109,105,110,64,97,116,101,110,118,101,108,100,116,46,111,114,103,34,62,119,101,98,109,105,110,64,97,116,101,110,118,101,108,100,116,46,111,114,103,60,47,97,62);
    };
// -->
</script>

或者,当您设置了 jQuery 时:

<p>My email address is <span id="email"></span>.</p>
<script type="text/javascript">
<!--
    $( function() {
        $("#email").text(String.fromCharCode(60,97,32,104,114,101,102,61,34,109,97,105,108,116,111,58,119,101,98,109,105,110,64,97,116,101,110,118,101,108,100,116,46,111,114,103,34,62,119,101,98,109,105,110,64,97,116,101,110,118,101,108,100,116,46,111,114,103,60,47,97,62));
    } );
// -->
</script>

I don't know if this is what happened, but document.write and document.writeln will not work if your site tells the browser it is strict XHTML. I believe that for this to happen, you have to use the strict DOCTYPE and set the Content-Type header to application/xml+xhtml rather than text/html (the default under many servers). This is because manipulating the DOM in this way could break it. For example, if I put the following half-way down a validated web page:

<script type="text/javascript">
<!--
    document.write("</body>");
// -->
</script>

The document would validate and be XHTML compliant, but would not work in most browsers.

The alternative is to create a DOM node where the email address should be inserted, and to insert it when the page has loaded. For example:

<p>My email address is <span id="email"></span>.</p>
<script type="text/javascript">
<!--
    document.body.onload = function() {
        document.getElementById("email").textContent = String.fromCharCode(60,97,32,104,114,101,102,61,34,109,97,105,108,116,111,58,119,101,98,109,105,110,64,97,116,101,110,118,101,108,100,116,46,111,114,103,34,62,119,101,98,109,105,110,64,97,116,101,110,118,101,108,100,116,46,111,114,103,60,47,97,62);
    };
// -->
</script>

Or, as you have jQuery set up:

<p>My email address is <span id="email"></span>.</p>
<script type="text/javascript">
<!--
    $( function() {
        $("#email").text(String.fromCharCode(60,97,32,104,114,101,102,61,34,109,97,105,108,116,111,58,119,101,98,109,105,110,64,97,116,101,110,118,101,108,100,116,46,111,114,103,34,62,119,101,98,109,105,110,64,97,116,101,110,118,101,108,100,116,46,111,114,103,60,47,97,62));
    } );
// -->
</script>
坐在坟头思考人生 2024-07-26 07:00:23

我想我在 DNN bug 跟踪器中找到了具体的答案:

输出应该是:

<script type="text/javascript">

//<![CDATA[
 document.write(String.fromCharCode(60,97,32,104,114,101,102,61,34,109,97,105,108,116,111,58,84,101,115,116,64,106,101,102,102,109,97,114,116,105,110,46,99,111,109,34,62,84,101,115,116,64,106,101,102,102,109,97,114,116,105,110,46,99,111,109,60,47,97,62))
//]]>

</script>

这似乎解决了我的网站(没有运行 XHTML)的问题。

该错误位于此处

I think I found the specific answer in the DNN bug tracker:

the output should be:

<script type="text/javascript">

//<![CDATA[
 document.write(String.fromCharCode(60,97,32,104,114,101,102,61,34,109,97,105,108,116,111,58,84,101,115,116,64,106,101,102,102,109,97,114,116,105,110,46,99,111,109,34,62,84,101,115,116,64,106,101,102,102,109,97,114,116,105,110,46,99,111,109,60,47,97,62))
//]]>

</script>

This seems to fix the issue for my site (which wasn't running XHTML).

The bug is located here.

一刻暧昧 2024-07-26 07:00:23

杰夫,

您确实找到了正确的解决方案,但说实话,我不太确定这样做的好处。 是的,电子邮件可能会被删除,但这个过程完全是多余的,至少在我看来是这样。 无需运行 JavaScript,只需呈现电子邮件链接即可。

这只是我为您手头的具体问题支付的 0.02 美元。

Jeff,

You did find the right solution, but to be honest, I'm not really sure of the benefit of this. Yes, e-mails can get scraped, but this process is just downright overkill, at least in my opinion. There is no need to have javascript running, just to render an e-mail link.

That is just my $0.02 on your very specific matter at hand.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文