browser icon indicating copy to clipboard operation
browser copied to clipboard

`a` tags without `href` generate a navigation event

Open sporto opened this issue 7 years ago • 6 comments

When using Browser.application. If you have an a which doesn't have an href, Elm will generate a navigation event.

Reproduction here: https://github.com/sporto/repro-elm-application-a-tag/blob/master/src/Main.elm

This seems like a bug, a tag is not supposed to be a link unless it has a href.

https://www.w3.org/TR/html5/text-level-semantics.html#the-a-element

If the a element has an href attribute, then it represents a hyperlink (a hypertext anchor) labeled by its contents.

If the a element has no href attribute, then the element represents a placeholder for where a link might otherwise have been placed, if it had been relevant, consisting of just the element’s contents.

sporto avatar Sep 13 '18 02:09 sporto

A good example for that is an in-page-anchor.

<a href="#someAnchor">Click here to jump to anchor</a>

<!-- An anchor on the page without href -->
<a name="someAnchor"></a>

ChristophP avatar Sep 18 '18 07:09 ChristophP

@sporto If you want to test all sorts of behaviors out, I recommend this gist https://gist.github.com/Pilatch/c1b6e5d925c6479a0b2e53892fa2dca3

I have discovered the culprit, and it's not in elm/browser. It's in elm/virtual-dom.

Pull request created for the fix - https://github.com/elm/virtual-dom/pull/142

Pilatch avatar Oct 03 '18 17:10 Pilatch

For the record, it maps to External ""

domenkozar avatar Mar 27 '19 10:03 domenkozar

for anyone here looking to get past this thing. The following delivers the desired behavior

onClickAnchor :: msg -> List (Attribute msg)
onClickAnchor msg = [ preventDefaultOn "click" <| Json.succeed (msg,True), href "#" ]

Fresheyeball avatar Oct 14 '19 23:10 Fresheyeball

Just ran into this again. The current behavior is really bad. It basically makes using in page anchor's useless with Browser.application, because you don't want the page to reload there. It would be such an easy fix to just exclude links without an href attribute from the special treatment.

ChristophP avatar Aug 17 '20 11:08 ChristophP

for anyone here looking to get past this thing. The following delivers the desired behavior

onClickAnchor :: msg -> List (Attribute msg)
onClickAnchor msg = [ preventDefaultOn "click" <| Json.succeed (msg,True), href "#" ]

How to use it? Could you please share an example?

pruchay avatar Apr 14 '22 06:04 pruchay