7

Recently I came across something like this

<a href="javascript://">some link</a>

I have no clue what "javascript://" means in that code. Does it mean a protocol named "javascript"?

Any help is greatly appreciated.

JBT
  • 8,498
  • 18
  • 65
  • 104
  • 4
    Maybe it executes the instruction after the colon. // is a comment, so maybe it does nothing. – antoyo May 17 '13 at 15:41
  • 1
    To me it looks erroneous, if want a link to nop you can do `javascript:;` or `javascript:void(0);`, haven't seen `javascript://`. What @antoyo said sounds plausible to me. – Matthew May 17 '13 at 15:42
  • http://stackoverflow.com/questions/7755088/href-expression-a-href-javascript-a – Marcin Wasiluk May 17 '13 at 15:43
  • 3
    it means that someone isn't following good practice. – Spudley May 17 '13 at 15:44
  • JavaScript isn't and hasn't' got a *protocol*, thus the content inside the `href` might work because of some Framework that will be able to make something from the value of that attribute. Otherwise a common (popular) browser will not like this notation. –  May 17 '13 at 15:48
  • If it's inside HTML 5 iPhone / Android application it can be catch by the device and used to do some native stuff before / after or instead of a request. – Дамян Станчев May 17 '13 at 15:49
  • possible duplicate of [What does the JavaScript pseudo protocol actually do?](http://stackoverflow.com/questions/10068781/what-does-the-javascript-pseudo-protocol-actually-do) – Bergi May 17 '13 at 15:56
  • @Bergi definitely not a duplicate - that SO question does not refer to there being an existance or not of a `javascript://` protocol – ddavison May 17 '13 at 16:04

2 Answers2

12

Further looking into it, javascript:// is not a valid protocol.

Typically when you want to execute js via a link, you use javascript:doSomething();.

In this case,

  • Let javascript: mean "execute Javascript code after the :"
  • And let // mean a Javascript comment.

It seems to be a placeholder to do nothing, just as javascript:; would do.

So literally: execute // (do nothing)

ddavison
  • 28,221
  • 15
  • 85
  • 110
  • The only logical reason someone might have used this notation on purpose would be some kind of *Framework*-ish notation so it can handle particular actions/request in it's own way. No other reason could really be logical. –  May 17 '13 at 15:51
  • Eh. I prefer having a link with a class and a real href, and attaching an onclick handler via script. At that point, one can actually use the href to determine the intended action, and in many cases it can be made to work without JS as well. – cHao May 17 '13 at 15:53
  • @cHao and that freedom lies with you! thankfully programming operates in the way you want it to. – ddavison May 17 '13 at 15:54
  • 2
    @sircapsalot: Except that `#` will jump you to the top of the page, so the behavior isn't identical. –  May 17 '13 at 15:54
  • I've seen enough weirdos build internal (company) frameworks with these ridiculous (and confusing) behavior :P. –  May 17 '13 at 15:56
  • 1
    Check this answer for the correct way of doing so: http://stackoverflow.com/questions/134845/href-attribute-for-javascript-links-or-javascriptvoid0 – Matanya May 17 '13 at 16:04
2

it leads to nowhere as no url is specified.

There are some other approach to the same thing:

href="#" adds an extra entry to the browser history (which is annoying when e.g. back-buttoning).

href="" reloads the page

href="javascript:;" does not seem to have any problems (other than looking messy and meaningless)

CuriousMind
  • 33,537
  • 28
  • 98
  • 137
  • 1
    A link that isn't actually intended to navigate anywhere will also nearly always have an onclick handler that prevents the default action, so the href really doesn't matter. Such a link rather sucks, though, as there's basically no way to make it work without JS enabled. – cHao May 17 '13 at 15:49