优雅编码,如何处理隐藏/不可见的HTML代码?

发布于 2024-08-16 04:18:02 字数 415 浏览 4 评论 0原文

我正在开发一个问答网站。每个答案旁边都有一个“选择最佳答案”按钮,该按钮应该对提问者可见,但对其他查看者不可见。网页的其他部分几乎是一样的。那么我该如何编码这个网页呢?我是否应该每次检查查看者身份以确定按钮是否可见。如果一个问题有很多答案,那么除了提问者之外,还有很多按钮对观众隐藏。这种编码会浪费很多额外的带宽吗?我认为这种编码有点可疑。有更好的方法吗?

“添加评论”按钮也存在同样的问题。正下方有一个“添加评论”按钮和一个隐藏的

,如果有 20 个答案,则有 20 个隐藏
,隐藏代码大大增加了HTML文件的大小。有没有一种优雅的方式呢?

至于“添加评论”按钮,任何登录用户都可以添加评论,因此不存在身份差异。那么如何处理这个问题呢?

I am developing an ask-and-answer website. There is a "Choose as best answer" button besides each answer, this button should be visible to the asker but should be invisible to other viewers. Other part of the web page is almost the same. So how can I code this web page? Should I check the viewer identity every time to determine whether or not the button should be visible. If there are many answers to a question, there are many buttons hidden from viewers except the asker. Does this kind of coding waste a lot of extra bandwidth? And I think this kind of coding is a little bit fishy. Is there a better way to do it?

The same problem goes with the "add comment" button. Right below there is an "Add comment" button and a hidden <form><textarea></textarea></form>, if there are 20 answers, there would be 20 hidden <form></form>, the hidden code greatly increase the size of the HTML file. Is there an elegant way instead?

As for "add comment" button, any logged user can add comment, so there is no identity differentiation. So how to deal with the issue?

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

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

发布评论

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

评论(4

山有枢 2024-08-23 04:18:02

这种类型的东西不会浪费带宽,不是。是的,您应该仅在当前查看页面的用户可以使用这些控件时才在页面上包含这些控件,因此请务必检查您的登录(如果他们已登录)用户是谁以及他们的关系是什么他们正在查看的项目。

这可能很简单:

if ($current_user_id == $question_asker_id) {
  // show 'accept as best answer' form
}

在允许对任何内容进行任何更改之前,请务必检查服务器端的表单数据。

This type of stuff isn't a waste of bandwidth, no. And yes, you should only include those controls on the page if they are available to the user presently viewing the page, so be sure to check who your logged-in (if they're logged-in) user is and what their relationship is to the item they're viewing.

This could be as simple as:

if ($current_user_id == $question_asker_id) {
  // show 'accept as best answer' form
}

Be sure to check the form-data on the server-side too before permitting any changes to anything.

看海 2024-08-23 04:18:02

您不应该隐藏 HTML,您应该不包含 HTML。即在服务器上,您正在执行以下操作:

if ($loggedIn && $user == 'asker') {  // pseudocode
    echo acceptButton();  // outputs the HTML for the button
}

非询问者用户甚至不会在浏览器中收到接受按钮的 HTML。

You're not supposed to be hiding the HTML, you're supposed to not include the HTML. I.e. on the server, you're doing something like this:

if ($loggedIn && $user == 'asker') {  // pseudocode
    echo acceptButton();  // outputs the HTML for the button
}

Non-asker users will not even receive the HTML for the accept button in their browser.

晨曦÷微暖 2024-08-23 04:18:02

我是否应该每次检查查看者身份以确定按钮是否可见。

是的,检查然后不显示按钮。此外,您还应该检查按钮被按下后以及处理该操作时是否不是询问者发起的,因为这很容易被滥用。

Should I check the viewer identity every time to determine whether or not the button should be visible.

Yes, check and then don't show the button. In addition you should also check once the button is pushed and it once you are processing that action that it's not the asker who initiated it because that is easily abused.

流云如水 2024-08-23 04:18:02

如果您担心 20 个小表单占用太多空间(提示,它们不会,大多数服务器都会压缩其输出,因此额外的表单几乎被压缩掉),只需使用简单的链接即可。

要隐藏,只需使用 if 测试,您应该检查用户的身份来执行此操作。这条 if 语句的开销(即使运行 20 次)几乎为零。

“过早的优化是万恶之源”——学习这个,生活这个。

执行此操作的“优雅”方法是(伪代码):

foreach($answers as $answer)
{
    // Print answer here
    if($current_user == $question_asker)
    {
        echo "button form here";
    }
    echo "comment form here";
}

If you're concerned with 20 minor forms taking much space (hint, they don't, most servers compress their output, so the extra forms are pretty much compressed away), just use simple links.

For hiding just use if tests and you should check the identity of the user to do this. The overhead of this single if statement (even if ran 20 times) is pretty much zero.

"Premature optimization is the root of all evil" - learn this, live this.

The "elegant" way to do this would be (pseudocode):

foreach($answers as $answer)
{
    // Print answer here
    if($current_user == $question_asker)
    {
        echo "button form here";
    }
    echo "comment form here";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文