2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-04-28 16:49:39 +02:00
There are two ways to build a link that has the sole purpose of running JavaScript code. The goal of this rule is to ban such patterns in order to support browsing with JavaScript disabled.
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,html]
2021-04-28 16:49:39 +02:00
----
<a href="#" onclick="alert('Clicked!'); return false;">Run JavaScript Code</a> <!-- Noncompliant -->
<a href="javascript:void(0)" onclick="alert('Clicked!'); return false;">Run JavaScript Code</a> <!-- Noncompliant -->
<a id="inPageAnchor">Jump down the page to me</a> <!-- Compliant -->
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,html]
2021-04-28 16:49:39 +02:00
----
<a id="inPageAnchor">Jump down the page to me</a> <!-- Compliant -->
----
2021-04-28 18:08:03 +02:00
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Replace this 'href' value by a link to an existing page or anchor.
2021-09-20 15:38:42 +02:00
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== on 19 Jun 2013, 09:16:25 Dinesh Bolkensteyn wrote:
It seems that there is a difference between the 2 patterns.
* # will scroll back up to the top of the page.
* javascript:void(0) does not affect the scrollbar.
But indeed with the return false; in the onclick, both do the same.
=== on 19 Jun 2013, 09:48:12 Dinesh Bolkensteyn wrote:
I'm wondering if this rule should also apply to href="" or not.
=== on 8 Jul 2013, 18:25:37 Freddy Mallet wrote:
Is implemented by \http://jira.codehaus.org/browse/SONARPLUGINS-2984
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]