使用 javascript 调用带有 javascript 的链接的 HREF 属性!

发布于 2024-09-18 20:13:48 字数 667 浏览 6 评论 0原文

我以前从未见过这个,但如果 HREF 包含 javascript:;//code......; 则可以使用 javascript 调用链接的 HREF 属性。

在我下面的示例中,单击两个链接。尽管 HREF 中的 javascript 不同,但它们执行相同的操作。

例如:

     <script type="text/javascript">
            function clickme()
            {
                var link = document.getElementById("clickme");
                eval(link.href);
            }
    </script>
    <a id="clickme" href="javascript:alert('hello');">I will alert hello</a>
    <br />
    <a href="javascript:clickme()">click me</a>

我在 IE8、Firefox 3.6.8、Safari 5.0.1 和 Chrome 6.0.472.55 上进行了测试。这是标准化的吗,这样我就不用担心这个功能将来会被弃用吗?

I never seen this before but you can invoke the HREF attribute of a link using javascript if the HREF contains javascript:;//code......;

On my example below click on both links. they do the same thing even though they have different javascript in the HREF.

for example:

     <script type="text/javascript">
            function clickme()
            {
                var link = document.getElementById("clickme");
                eval(link.href);
            }
    </script>
    <a id="clickme" href="javascript:alert('hello');">I will alert hello</a>
    <br />
    <a href="javascript:clickme()">click me</a>

I tested this on IE8, Firefox 3.6.8, Safari 5.0.1, and Chrome 6.0.472.55. Is this standardized, so I will not have to worry about this feature being deprecated in the future?

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

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

发布评论

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

评论(3

橙幽之幻 2024-09-25 20:13:48

您不必担心它将来会被弃用。 现在这是一个坏主意。

实际情况是这样的:有一个使用 javascript: 协议的链接,该协议受到浏览器的尊重。这意味着 javascript: 后面的所有内容都是 JavaScript,应该由 JS 解释器执行。

当您检索链接的 href 时,您会收到一个字符串,例如“javascript:clickme()”。您可以在字符串上使用 eval 来执行 JavaScript。现在,您可能认为这会失败(因为前面的 javascript: 协议),但事实并非如此,因为 JavaScript 有标签,看起来像当您将其视为 JavaScript 代码时的标签。

所以它有效,但这是一个坏主意。在最新版本的 JavaScript(ECMAScript 第五版)的新“严格”模式中,它也是不允许的(因为 eval)。

一般来说,当我们认为需要使用 eval 来处理某些事情时,这表明我们的代码存在问题,并且需要进行一些重构。该规则的例外是我们大多数人永远不会遇到的非常边缘的情况。在这种情况下,它不应该让 href 属性包含我们想要执行的代码,而应该使用我们想要执行的代码。例如,您的示例将 clickMe 函数作为唯一使用的函数。我们应该直接调用该函数,而不是eval它。

You don't have to worry about it being deprecated in the future. It's a bad idea now.

What's really happening there is this: There's a link using the javascript: protocol, which is respected by browsers. It means that everything following the javascript: is JavaScript and should be executed by the JS interpreter.

When you retrieve the link's href, you receive it as a string, e.g. "javascript:clickme()". You can use eval on strings to execute JavaScript. Now, you'd think that would fail (because of the javascript: protocol thing at the front), but it doesn't, because JavaScript has labels and that looks like a label when you treat it as JavaScript code.

So it works, but it's a bad idea. It's also disallowed (because of the eval) in the new "strict" mode of the latest version of JavaScript, ECMAScript 5th edition.

In general, when we think we need to use eval for something, it indicates that there's a problem with our code and that some refactoring is in order. The exceptions to that rule are very edgey edge cases that most of us will never run into. In this case, rather than having the href attribute contain the code that we want to execute, it should just use the code we want to execute. Your example, for instance, has a clickMe function as the only thing being used. Rather than evaling it, we should just call that function directly.

沉默的熊 2024-09-25 20:13:48

它不会被弃用,但我看不到它的用途。

如果您确实想简化此操作,请执行以下操作:

 <script type="text/javascript">
            function clickme()
            {
                 clicked();
            }
            function clicked()
            {
                alert("hello");
            }

    </script>
<a id="clickme" href="javascript:clicked();">I will alert hello</a>
<br />
<a href="javascript:clickme()">click me</a>

或者更好地执行:

  <a href="#" onclick="clicked();">Click Me</a>

甚至更好:

<span onclick="clicked();" class="MakeMeLookLIkeLInk">Click Me</a>

锚点控件主要用于导航,并且是保持这种方式的良好实践。如果您有需要执行的功能,请使用带有 onclick 的 span/div。您也可以使用按钮。

It won't be deprecated but I don't see the use of it.

If you do want to stream line this more do:

 <script type="text/javascript">
            function clickme()
            {
                 clicked();
            }
            function clicked()
            {
                alert("hello");
            }

    </script>
<a id="clickme" href="javascript:clicked();">I will alert hello</a>
<br />
<a href="javascript:clickme()">click me</a>

Or better yet do:

  <a href="#" onclick="clicked();">Click Me</a>

Or even better:

<span onclick="clicked();" class="MakeMeLookLIkeLInk">Click Me</a>

Anchor controls are mainly used for navigation, and as a good practice to keep it that way. if you have functionality that needs to take place, use a span/div with an onclick. You can use a button as well.

幽梦紫曦~ 2024-09-25 20:13:48

我认为你的问题是这条线是否

eval(link.href);

有效。

答案是肯定的。您没有理由不能评估存储在某些属性中的代码,就像评估输入框的内容一样。

也就是说,这看起来是一种非常糟糕的编码方式。如果你不小心,你可能会陷入循环。此外,这样的代码将很难维护。

I think your question is whether the line

eval(link.href);

is valid.

The answer to that is yes, it is. There's no reason you can't evaluate code that's stored in some property, the same way you could evaluate the contents of an input box.

That said, this looks like a VERY bad way to code things. If you're not careful, you could end up in a circular loop. Additionally, code like this will be very hard to maintain.

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