Re: javascript: protocol

"Jon Hanna" <jon@spinsol.com>

> My question is, what exactly is the problem with it.

There are huge problems with it in simply a javascript context, from a
script authoring perspective it's one of the most common failings "why
doesn't this work?" questions in javascript can very often be solved
simply by stopping using it - http://jibbering.com/faq/#FAQ4_24 - (if you
want details of scenarios where it causes problems feel free off-list.)

> I also acknowledge that it is rarely necessary (the only thing I can
think
> of that can't be done another way is using it in the src of an img
element,
> something I've only seen done once, and then as an example).

That's not actually feasible in many browsers even those that support the
javascript pseudo protocol, using it for image editor is very limited.

> However in a case where javascript is clearly available (something
written
> from JavaScript for example). It is a slightly more eloquent and
> self-documenting syntax. So do people still see a problem with it in
that
> case, and if so what exactly?

The existence of javascript does not imply existence of the javascript
psuedo protocol, there are a number of browsers which don't have it - and
it's even possible to configure IE such that it supports javascript but
not the protocol (and there's at least one system in the world where this
has happened.)  There's also the case that javascript support does not
necessarily mean support for your function, and you need to have some
fallback for when your functionality is not supported.

Consider:

<script>
if (window.open && document && document.write) {
 document.write('<a
href="javascript:window.open(\'javascript:\\\'hello\\\'\');void
0">Hello</a>')
}
</script>

If the javascript: protocol isn't supported you get a protocol error,
which are particularly incomprehensible in almost every browser I've seen
or if it is, and you say try to open it in a window yourself (which is a
common occurance for links which authors want to open in new windows.)
then you end up with 3 windows open, one completely blank - if it was a
function that you referenced on the page (rather than window.open) you'd
end up with 2 windows open one of which is giving a javascript error.

You simply do not need to use the protocol, so as it's possible to fail,
and it causes scripting problems you should avoid it.

Jim.

Received on Friday, 1 March 2002 07:32:00 UTC