Is there a way to create an empty link in D8? What I want specifically is

<a href="javascript:void(0)">Link</a>

Url::fromUserInput('#', ['fragment' => '#']) creates an anchor tag with ## link.


  • 2
    Possible duplicate of Create a link pointing to # Aug 30, 2018 at 18:38
  • Might not be. The solution in the link creates a # link. I want a javascript: void(0) linked anchor tag. Aug 31, 2018 at 3:38
  • 1
    Because of what you tried I thought it might be. Either way, I expect the answer will be the same: use an inline or twig template to do this. The Url class - from a cursory glance at the source - I don't think will generate a javascript url. Aug 31, 2018 at 7:16
  • A possible way out might be like this - $nolink_url = Url::fromUserInput('#', ['fragment' => '#', 'attributes' => ['onclick' => 'return false']]); Though this would not put a javascript: void(0) in place, but will stop the jump to the top which was my actual requirement. :) Aug 12, 2021 at 7:19


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.