Juri
Juri

Reputation: 331

"javascript:" as value of which HTML attributes

I saw that "javascript:" works as values ​​of the HTML attributes href and action.

Apart from the opportunity of this solution, and regardless of the fact that there are event attributes (onclick, etc.), does "javascript:" work with other HTML attributes?

(for example, the HTML title attribute displays it as "plain text")

<a href="javascript:alert('href');">LINK</a>

<form action="javascript:alert('action');">
    <button type=submit>button</button> <!--(it works in some web browsers) -->
</form>

<p title="javascript:alert('title');">title</p>

Upvotes: 0

Views: 49

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1075039

javascript: is a pseudo-protocol which you can use essentially anywhere you can put a URL. That's why it works in href on a elements and action on form elements, which are attributes in which URLs are expected, and not in title, which isn't expected to contain a URL. But having just tried it on an img's src and had it not work (in Chrome), I wouldn't be surprised if some URL attributes are locked down. It may well only be ones with an associated activation from a user event where browsers run the code.

That said, you've identified just about the only two places where it would be useful. :-)

Upvotes: 3

Related Questions