简体   繁体   English

在标签中调用javascript函数的更好方法

[英]Better way to call javascript function in a tag

Which of the following ways is a better way to call a js function from an a tag? 以下哪种方法是从标签调用js函数的更好方法?

<a href="javascript:someFunction()">LINK</a>

OR 要么

<a href="#" onclick="someFunction();" return false;">LINK</a>

I have seen this question here , but it says <span onclick="someFunction()"> is a better option. 我在这里看到了这个问题 ,但它说<span onclick="someFunction()">是一个更好的选择。 But due to some reasons I have to use <a> links. 但由于某些原因,我必须使用<a>链接。

EDIT: I am looking for a cross browser & cross platform solution which should work on androids & iPads too. 编辑:我正在寻找一个跨浏览器和跨平台的解决方案,它也适用于机器人和iPad。

Neither is good. 两者都不好。

Behaviour should be configured independent of the actual markup. 应该独立于实际标记配置行为。 For instance, in jQuery you might do something like 例如,在jQuery中你可能会做类似的事情

$('#the-element').click(function () { /* perform action here */ });

in a separate <script> block. 在一个单独的<script>块中。

The advantage of this is that it 这样做的好处就是它

  1. Separates markup and behaviour in the same way that CSS separates markup and style 以与CSS分隔标记和样式相同的方式分隔标记和行为
  2. Centralises configuration (this is somewhat a corollary of 1). 集中配置(这有点是1的推论)。
  3. Is trivially extensible to include more than one argument using jQuery's powerful selector syntax 使用jQuery强大的选择器语法,可以轻松扩展以包含多个参数

Furthermore, it degrades gracefully (but so would using the onclick event) since you can provide the link tags with a href in case the user doesn't have JavaScript enabled. 此外,它会优雅地降级(但使用onclick事件也是如此),因为如果用户没有启用JavaScript,您可以为链接标记提供href

Of course, these arguments still count if you're not using jQuery or another JavaScript library (but why do that?). 当然,如果您不使用jQuery或其他JavaScript库(但为什么这样做?),这些参数仍然很重要。

Some advantages to the second option: 第二种选择的一些优点:

  1. You can use this inside onclick to reference the anchor itself (doing the same in option 1 will give you window instead). 你可以在onclick里面使用this来引用锚本身(在选项1中做同样的事情会给你一个window )。

  2. You can set the href to a non-JS compatible URL to support older browsers (or those that have JS disabled); 您可以将href设置为非JS兼容的URL,以支持旧版浏览器(或禁用JS的浏览器); browsers that support JavaScript will execute the function instead (to stay on the page you have to use onclick="return someFunction();" and return false from inside the function or onclick="return someFunction(); return false;" to prevent default action). 支持JavaScript的浏览器将执行该函数(保留在页面上你必须使用onclick="return someFunction();"并从函数内部return falseonclick="return someFunction(); return false;" to prevent默认动作)。

  3. I've seen weird stuff happen when using href="javascript:someFunction()" and the function returns a value; 我发现当使用href="javascript:someFunction()"并且函数返回一个值时会发生奇怪的事情; the whole page would get replaced by just that value. 整个页面将被该值替换。

Pitfalls 陷阱

Inline code: 内联代码:

  1. Runs in document scope as opposed to code defined inside <script> tags which runs in window scope; document范围内运行,而不是在window范围内运行的<script>标记内定义的代码; therefore, symbols may be resolved based on an element's name or id attribute, causing the unintended effect of attempting to treat an element as a function. 因此,可以基于元素的nameid属性来解析符号,从而导致尝试将元素视为函数的意外效果。

  2. Is harder to reuse; 更难重用; delicate copy-paste is required to move it from one project to another. 需要精细的复制粘贴才能将其从一个项目移动到另一个项目。

  3. Adds weight to your pages, whereas external code files can be cached by the browser. 为页面添加权重,而外部代码文件可以由浏览器缓存。

I'm tempted to say that both are bad practices. 我很想说两者都是不好的做法。

The use of onclick or javascript: should be dismissed in favor of listening to events from outside scripts, allowing for a better separation between markup and logic and thus leading to less repeated code. 使用onclickjavascript:应该被解雇,以支持从外部脚本中侦听事件,从而允许更好地分离标记和逻辑,从而减少重复代码。

Note also that external scripts get cached by the browser. 另请注意,外部脚本会被浏览器缓存。

Have a look at this answer . 看看这个答案

Some good ways of implementing cross-browser event listeners here . 这里实现跨浏览器事件侦听器的一些好方法。

Modern browsers support a Content Security Policy or CSP. 现代浏览器支持内容安全策略或CSP。 This is the highest level of web security and strongly recommended if you can apply it because it completely blocks all XSS attacks . 这是最高级别的Web安全性,强烈建议您应用它,因为它完全阻止了所有XSS攻击

Both of your suggestions break with CSP enabled because they allow inline Javascript (which could be injected by a hacker) to execute in your page. 你的两个建议都打破了CSP,因为它们允许内联Javascript(可以由黑客注入)在你的页面中执行。

The best practice is to subscribe to the event in Javascript, as in Konrad Rudolph's answer. 最佳做法是在Javascript中订阅该事件,如Konrad Rudolph的回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM