使用javascript调用带有javascript的链接的HREF属性! [英] Invoking the HREF attribute of a link with javascript using javascript!

查看:264
本文介绍了使用javascript调用带有javascript的链接的HREF属性!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前从未见过,但是如果HREF包含javascript:; // code ......;

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

在下面的示例中,单击两个链接。即使在HREF中使用不同的javascript,他们也会做同样的事情。

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

例如:

     <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 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?

推荐答案

您没有担心将来会弃用它。 现在是个坏主意。

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

真正发生的是:使用 javascript的链接:协议,该协议受到浏览器的尊重。这意味着 javascript:之后的所有内容都是JavaScript,应由JS解释器执行。

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.

检索时链接的href,您将其作为字符串接收,例如 javascript:clickme()。您可以在字符串上使用 eval 来执行JavaScript。现在,您可能认为这样做会失败(因为前面的 javascript:协议是必需的),但是不会,因为JavaScript具有 labels ,当您将其视为JavaScript代码时,它看起来像是一个标签。

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.

因此它可以工作,但这不是一个好主意。 (由于 eval )在最新版本的JavaScript ECMAScript第5版的新严格模式下也是不允许的。

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.

通常,当我们认为需要对某些内容使用 eval 时,表明我们的代码有问题,并且需要进行一些重构。该规则的例外是非常边缘的情况,我们大多数人都不会碰到。在这种情况下,与其让 href 属性包含我们要执行的代码,不如让它 use 我们要执行的代码。例如,您的示例仅使用 clickMe 函数。我们应该直接调用该函数,而不是 eval

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.

这篇关于使用javascript调用带有javascript的链接的HREF属性!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆