使用 javascript 优雅地降级锚标记

发布于 2024-07-12 10:06:15 字数 624 浏览 7 评论 0原文

我目前依靠锚标记在我的 Web 应用程序上执行 AJAX 请求(使用 jQuery)。 例如:

<script type="text/javascript">
    $(document).ready(function() {
        $("#test").click(function() {
            // Perform AJAX call and manipulate the DOM
        });
    });

    <a id="test" href="">Click me!</a>
</script>

但是,如果用户在浏览器中禁用 JavaScript,这些锚标记几乎变得毫无用处。 处理这些锚标签的优雅降级的最佳方法是什么? 我唯一的选择是将它们全部切换到表单标签内的按钮吗?

编辑:我应该更清楚地指出,我的 AJAX 调用是对 URL 的 HTTP POST,出于安全原因,我无法将其公开给正常的 HTTP GET(例如,考虑删除 url“/items/delete/1”)。 考虑到这一点,我无法将锚点的 href 更改为“/items/delete/1”以满足关闭 JavaScript 的用户,因为它会带来安全风险。

I currently rely on anchor tags to perform AJAX requests on my web application (using jQuery). For example:

<script type="text/javascript">
    $(document).ready(function() {
        $("#test").click(function() {
            // Perform AJAX call and manipulate the DOM
        });
    });

    <a id="test" href="">Click me!</a>
</script>

However, these anchor tags pretty much become useless if the user disables javascript in their browser. What's the best way to handle graceful degradation of these anchor tags? Is my only option to switch them all to buttons inside form tags?

Edit: I should be more clear and specify that my AJAX call is an HTTP POST to a URL that I cannot, for security reasons, expose to normal HTTP GETs (e.g. think of a delete url "/items/delete/1"). With that in mind, I can't change the href of the anchor to be "/items/delete/1" in order to satisfy users with javascript turned off since it poses a security risk.

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

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

发布评论

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

评论(5

谁的年少不轻狂 2024-07-19 10:06:15

一种选择(我确信我会被否决)是不要打扰那些关闭了 javascript 的人。 在 标记中显示一条消息,通知用户您的网站需要 Javascript。

它将影响两类人:那些故意禁用了 JavaScript 并且可能知道如何重新打开它的人,以及那些在旧移动设备上查看您的网站的人。

在您继续花费大量时间为他们进行开发之前,请考虑一下这些人群的重要性。

One option (for which I'm sure I'll get downvoted), is to not bother with people who have javascript off. Display a message in a <noscript> tag informing the user that your site requires Javascript.

It'll affect two groups of people: those who have knowingly disabled javascript and presumably know how to turn it back on, and those who are viewing your site on an older mobile device.

Consider how important these groups of people are before you go on spending hours and hours developing for them.

水中月 2024-07-19 10:06:15

很简单,不要使用 javascript: URI 语法作为 href,至少不要在传递给客户端的 HTML 中使用。

从服务器传递 HTML,并使用 href 将用户带到您想要访问的任何页面,然后替换锚标记的 href(或捕获 onclick 事件)并执行您想要的任何操作,确保防止默认操作为事件起火。

<script type="text/javascript">
    $(document).ready(function() {
        $("#test").click(function(e) {
            // Perform AJAX call and manipulate the DOM
            e.preventDefault();
        });
    });

    <a id="test" href="/Proper/URI">Click me!</a>
</script>

我完全同意阅读 Unobtrusive JavaScript 的建议。

Simple, don't use the javascript: URI syntax as the href, at least, not in the HTML delivered to the client.

Deliver the HTML from the server with the href taking the user to whatever page you want to take them, and then replace the href (or capture the onclick event) for the anchor tag and do whatever you wish, being sure to prevent the default action for the event fire.

<script type="text/javascript">
    $(document).ready(function() {
        $("#test").click(function(e) {
            // Perform AJAX call and manipulate the DOM
            e.preventDefault();
        });
    });

    <a id="test" href="/Proper/URI">Click me!</a>
</script>

And I fully agree with the suggestion to read Unobtrusive JavaScript.

染火枫林 2024-07-19 10:06:15

The answer I was looking for is buried in one of the comments:

your use of the <a> tag to do an HTTP
POST operation is conflicting. <a> was
designed for HTTP GET. You won't find
a satisfactory answer here if you keep
the semantics of your operation
embedded in an <a> tag. See
http://www.w3.org/2001/tag/doc/whenToUseGet.html

╰◇生如夏花灿烂 2024-07-19 10:06:15

阅读Unobtrusive JavaScript,了解如何将 JS 行为与实际标记分开。 我发布的任何其他内容都只是别人在其他地方说过的更好内容的重述

Read about Unobtrusive JavaScript to learn how to separate JS behavior from your actual markup. Anything else I post will just be a rehash of what someone else has said better elsewhere

静谧幽蓝 2024-07-19 10:06:15

对于 no-JS,最好的选择可能是拥有一个带有提交按钮的表单,并将表单的操作指向您的 URL。

您可以将按钮设置为看起来像链接(或其他任何内容):

<button class="linkstyle" type="submit">Foo</button>
<style type="text/css">
button.linkstyle { border:none; background:none; padding:0; }
<style>

Particletree 的文章中有一个很好的按钮样式示例:重新发现按钮元素

Your best bet for no-JS would probably be to have a form with a submit button, and point the form's action to your URL.

You can style the button to look like a link (or like anything else):

<button class="linkstyle" type="submit">Foo</button>
<style type="text/css">
button.linkstyle { border:none; background:none; padding:0; }
<style>

There's a good example of styling buttons in Particletree's article: Rediscovering the Button Element

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